DECLARE 
	@COD_MATRIZ_FISCAL	VARCHAR(6),
	@MATRIZ_FISCAL		VARCHAR(25),
	@DATA_INICIAL		DATETIME,
	@DATA_FINAL			DATETIME

-- PREENCHER AS VARIAVEIS ABAIXO PARA RODAR OS SELECTS
SELECT @COD_MATRIZ_FISCAL	= ''
SELECT @MATRIZ_FISCAL		= ''
SELECT @DATA_INICIAL		= ''
SELECT @DATA_FINAL			= ''

----------------------------------------------------------------------------------------------------------------------------------------------------
-- 0150
			SELECT DISTINCT * FROM (
				SELECT DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFE.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFE.CODIGO_CLIENTE)), RTRIM(LTRIM(LFE.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_ENTRADA_IMPOSTO AS LFE
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFE.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFE.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					ES.DESC_ESPECIE_SERIE		<>		'ECF'									AND
					LFE.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306',
														'1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356',
														'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
														'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
														'1932', '2932')							AND
					LFE.CODIGO_FISCAL_OPERACAO	<		'9000'									AND
					LFE.ID_IMPOSTO				IN		(1, 2, 12)								AND
					LFE.COD_MATRIZ_FISCAL		=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT 	DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFS.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFS.CODIGO_CLIENTE)), RTRIM(LTRIM(LFS.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_SAIDA_IMPOSTO AS LFS
					LEFT JOIN SERIES_NF AS S ON
						LFS.SERIE_NF = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFS.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFS.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFS.ESPECIE				NOT IN	('ECF', 'CF')							AND
					LFS.ID_IMPOSTO			IN		(1, 2, 12)								AND
					LFS.SERIE_NF			<>		'D1'									AND
					LFS.COD_MATRIZ_FISCAL	=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFE.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFE.CODIGO_CLIENTE)), RTRIM(LTRIM(LFE.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_ENTRADA_IMPOSTO AS LFE
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFE.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFE.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306',
														'1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356',
														'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
														'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
														'1932', '2932')							AND
					LFE.ID_IMPOSTO				IN		(1, 2, 12)								AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT 	DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFS.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFS.CODIGO_CLIENTE)), RTRIM(LTRIM(LFS.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_SAIDA_IMPOSTO AS LFS
					LEFT JOIN SERIES_NF AS S ON
						LFS.SERIE_NF = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFS.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFS.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFS.CODIGO_FISCAL_OPERACAO	NOT IN ('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
														'5256', '6256', '5257', '6257', '5258', '6258',
														'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
														'5306', '6306', '5307', '6307',
														'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
														'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
														'7350', '7358',
														'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
														'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
														'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
														'5932', '6932')							AND
					LFS.ID_IMPOSTO				IN		(1, 2, 12)								AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFE.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFE.CODIGO_CLIENTE)), RTRIM(LTRIM(LFE.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_ENTRADA_IMPOSTO AS LFE
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFE.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFE.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	IN		('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306')							AND
					LFE.ID_IMPOSTO				=		1										AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT 	DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFS.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFS.CODIGO_CLIENTE)), RTRIM(LTRIM(LFS.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_SAIDA_IMPOSTO AS LFS
					LEFT JOIN SERIES_NF AS S ON
						LFS.SERIE_NF = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFS.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFS.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFS.CODIGO_FISCAL_OPERACAO	IN		('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
														'5256', '6256', '5257', '6257', '5258', '6258',
														'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
														'5306', '6306', '5307', '6307')			AND
					LFS.ID_IMPOSTO				=		1										AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFE.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFE.CODIGO_CLIENTE)), RTRIM(LTRIM(LFE.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_ENTRADA_IMPOSTO AS LFE
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFE.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFE.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO		IN		('1350', '1352', '1353', '1354', '1355', '1356', '1932',
															'2350', '2352', '2353', '2354', '2355', '2356', '2932',
															'3350', '3352', '3353', '3354', '3355', '3356')	AND
					LFE.ID_IMPOSTO					=		1												AND
					LFE.COD_FILIAL					=		'<<Alltrim(.PX_COD_CLIFOR)>>'					AND
					LFE.RECEBIMENTO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'			AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT 	DISTINCT
					REG			=	'|0150|',
					COD_PART	=	CASE WHEN LFS.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(ISNULL(RTRIM(LTRIM(LFS.CODIGO_CLIENTE)), RTRIM(LTRIM(LFS.COD_CLIFOR))), '') END + '|',
					NOME		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CLIENTE_VAREJO)), RTRIM(LTRIM(C.RAZAO_SOCIAL))), '') + '|',
					COD_PAIS	=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 2, 1, ISNULL(CV.PAIS, C.PAIS)))), '') + '|',
					CNPJ		=	CASE WHEN C.PJ_PF = 1 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CPF			=	CASE WHEN C.PJ_PF = 0 THEN ISNULL(ISNULL(RTRIM(LTRIM(CV.CPF_CGC)), RTRIM(LTRIM(C.CGC_CPF))), '') ELSE '' END + '|',
					CEI			=	'0|',
					NIT			=	'0|',
					UF			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.UF)), RTRIM(LTRIM(C.UF))), '') + '|',
					IE			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.RG_IE)), RTRIM(LTRIM(C.RG_IE))), '') + '|',
					IE_ST		=	'|',
					COD_MUN		=	ISNULL(RTRIM(LTRIM(DBO.FX_LF_VALOR_INTEGRACAO(2, 3, 1, ISNULL(CV.CIDADE, C.CIDADE)))), '') + '|',
					IM			=	'|',
					SUFRAMA		=	'|',
					CEP			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.CEP)), RTRIM(LTRIM(C.CEP))), '') + '|',
					ENDERECO	=	ISNULL(ISNULL(RTRIM(LTRIM(CV.ENDERECO)), RTRIM(LTRIM(C.ENDERECO))), '') + '|',
					NUM			=	'|',
					COMPL		=	'|',
					BAIRRO		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.BAIRRO)), RTRIM(LTRIM(C.BAIRRO))), '') + '|',
					CEP_CP		=	'|',
					CP			=	'|',
					FONE		=	ISNULL(ISNULL(RTRIM(LTRIM(CV.TELEFONE)), RTRIM(LTRIM(C.TELEFONE1))), '') + '|',
					FAX			=	ISNULL(ISNULL(RTRIM(LTRIM(CV.FAX)), RTRIM(LTRIM(C.FAX))), '') + '|'
				FROM
					W_LF_REGISTRO_SAIDA_IMPOSTO AS LFS
					LEFT JOIN SERIES_NF AS S ON
						LFS.SERIE_NF = S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE = S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFS.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN CLIENTES_VAREJO AS CV ON
						LFS.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
				WHERE
					LFS.CODIGO_FISCAL_OPERACAO	IN		('5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
														'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
														'7350', '7358')							AND
					LFS.ID_IMPOSTO				=		1										AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				) A
				WHERE A.COD_PART <> '|'
				ORDER BY COD_PART

----------------------------------------------------------------------------------------------------------------------------------------------------
-- 0200
			SELECT DISTINCT
				REG				=	MAX(REG),
				COD_ITEM		=	COD_ITEM,
				DESCR_ITEM		=	MAX(DESCR_ITEM),
				COD_GEN			=	MAX(COD_GEN),
				COD_LST			=	MAX(COD_LST)
			FROM (
		         SELECT DISTINCT --C300
					REG				=	'|0200|',
					COD_ITEM		=	ISNULL(RTRIM(LTRIM(CASE	WHEN MAX(LFEI.COD_TABELA_FILHA) IN ('C', 'A', NULL) THEN
																	RTRIM(LFEI.NF_ENTRADA) +
																	RTRIM(CAST(CAST(LFEI.ITEM_IMPRESSAO AS INT) AS VARCHAR(3)))+
																	RTRIM(CASE WHEN ISNUMERIC(MAX(LFEI.COD_CLIFOR)) = 1 THEN CAST(CAST(MAX(LFEI.COD_CLIFOR) AS INT) AS VARCHAR(6)) ELSE CAST(MAX(LFEI.COD_CLIFOR) AS VARCHAR(6)) END)
																WHEN MAX(LFEI.COD_TABELA_FILHA) IN ('P', 'R') THEN
																	'P'+RTRIM(MAX(LFEI.CODIGO_ITEM))
																WHEN MAX(LFEI.COD_TABELA_FILHA) IN ('M', 'T') THEN
																	'M'+RTRIM(MAX(LFEI.CODIGO_ITEM))
																ELSE
																	CASE	WHEN RTRIM(MAX(LFEI.CODIGO_ITEM)) = '' OR MAX(LFEI.CODIGO_ITEM) IS NULL THEN
																				RTRIM(LFEI.NF_ENTRADA) +
																				RTRIM(CAST(CAST(LFEI.ITEM_IMPRESSAO AS INT) AS VARCHAR(3)))+
																				RTRIM(CASE WHEN ISNUMERIC(MAX(LFEI.COD_CLIFOR)) = 1 THEN CAST(CAST(MAX(LFEI.COD_CLIFOR) AS INT) AS VARCHAR(6)) ELSE CAST(MAX(LFEI.COD_CLIFOR) AS VARCHAR(6)) END)
																			ELSE
																				MAX(LFEI.CODIGO_ITEM)
																	END
															END)), '') + '|',
					DESCR_ITEM		=	ISNULL(RTRIM(LTRIM(MAX(P.DESC_PRODUTO))), MAX(LFEI.DESCRICAO_ITEM)) + '|',
					COD_GEN			=	ISNULL(LEFT(P.CLASSIF_FISCAL, 2), LEFT(LFEI.CLASSIF_FISCAL, 2)) + '|',
					COD_LST			=	'|'
				FROM
					LF_REGISTRO_ENTRADA_ITEM AS LFEI
					LEFT JOIN LF_REGISTRO_ENTRADA AS LFE ON
						LFEI.NF_ENTRADA				=	LFE.NF_ENTRADA				AND
						LFEI.SERIE_NF_ENTRADA		=	LFE.SERIE_NF_ENTRADA		AND
						LFEI.TAXA_IMPOSTO			=	LFE.TAXA_IMPOSTO			AND
						LFEI.COD_CLIFOR				=	LFE.COD_CLIFOR				AND
						LFEI.COD_MATRIZ_FISCAL		=	LFE.COD_MATRIZ_FISCAL		AND
						LFEI.ID_IMPOSTO				=	LFE.ID_IMPOSTO				AND
						LFEI.CODIGO_FISCAL_OPERACAO	=	LFE.CODIGO_FISCAL_OPERACAO	AND
						LFEI.ITEM					=	LFE.ITEM
		            LEFT JOIN PRODUTOS P (NOLOCK)
			            ON LFEI.CODIGO_ITEM = P.PRODUTO
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA		=	S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFEI.COD_CLIFOR	=	C.COD_CLIFOR
				WHERE
					ES.DESC_ESPECIE_SERIE		<>		'ECF'									AND
					LFEI.ID_IMPOSTO				IN		(1, 2, 12)								AND
					LFEI.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306',
														'1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356',
														'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
														'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
														'1932', '2932')							AND
					LFEI.CODIGO_FISCAL_OPERACAO	<		'9000'									AND
					LFEI.COD_MATRIZ_FISCAL		=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFEI.NF_ENTRADA, LFEI.ITEM_IMPRESSAO, LFEI.CODIGO_ITEM, P.CLASSIF_FISCAL, LFEI.CLASSIF_FISCAL

			UNION ALL

		         SELECT DISTINCT --C300
					REG				=	'|0200|',
					COD_ITEM		=	ISNULL(RTRIM(LTRIM(CASE WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('C', 'A', NULL) THEN
																	RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('P', 'R') THEN
																	'P'+RTRIM(MAX(LFSI.CODIGO_ITEM))
																WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('M', 'T') THEN
																	'M'+RTRIM(MAX(LFSI.CODIGO_ITEM))
																ELSE
												 					CASE	WHEN RTRIM(MAX(LFSI.CODIGO_ITEM)) = '' OR MAX(LFSI.CODIGO_ITEM) IS NULL THEN
																				RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																			ELSE
																				MAX(LFSI.CODIGO_ITEM) END
															END)), '') + '|',
					DESCR_ITEM		=	ISNULL(RTRIM(LTRIM(MAX(P.DESC_PRODUTO))), MAX(LFSI.DESCRICAO_ITEM)) + '|',
					COD_GEN			=	ISNULL(LEFT(P.CLASSIF_FISCAL, 2), LEFT(LFSI.CLASSIF_FISCAL, 2)) + '|',
					COD_LST			=	'|'
				FROM
					LF_REGISTRO_SAIDA_ITEM AS LFSI
					LEFT JOIN LF_REGISTRO_SAIDA AS LFS ON
						LFSI.NF_SAIDA				=	LFS.NF_SAIDA				AND
						LFSI.SERIE_NF				=	LFS.SERIE_NF				AND
						LFSI.NF_SAIDA_FINAL			=	LFS.NF_SAIDA_FINAL			AND
						LFSI.TAXA_IMPOSTO			=	LFS.TAXA_IMPOSTO			AND
						LFSI.CODIGO_FISCAL_OPERACAO	=	LFS.CODIGO_FISCAL_OPERACAO	AND
						LFSI.ID_IMPOSTO				=	LFS.ID_IMPOSTO				AND
						LFSI.COD_MATRIZ_FISCAL		=	LFS.COD_MATRIZ_FISCAL		AND
						LFSI.COD_FILIAL				=	LFS.COD_FILIAL				AND
						LFSI.ITEM					=	LFS.ITEM
		            LEFT JOIN PRODUTOS P (NOLOCK)
			            ON LFSI.CODIGO_ITEM = P.PRODUTO
					LEFT JOIN SERIES_NF AS S ON
						LFS.SERIE_NF		=	S.SERIE_NF
					LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
						LFSI.COD_FILIAL		=	FILIAL.COD_CLIFOR
				WHERE
					LFS.ESPECIE				NOT IN	('ECF', 'CF')							AND
					LFSI.ID_IMPOSTO			IN		(1, 2, 12)								AND
					LFSI.SERIE_NF			<>		'D1'									AND
					LFSI.COD_FILIAL			=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFSI.NF_SAIDA, LFSI.SERIE_NF, LFSI.ITEM_IMPRESSAO, P.CLASSIF_FISCAL, LFSI.CLASSIF_FISCAL

			UNION ALL

		         SELECT DISTINCT --C555
					REG				=	'|0200|',
					COD_ITEM		=	ISNULL(RTRIM(LTRIM(CASE WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('C', 'A', NULL) THEN
																	RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('P', 'R') THEN
																	'P'+RTRIM(MAX(LFSI.CODIGO_ITEM))
																WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('M', 'T') THEN
																	'M'+RTRIM(MAX(LFSI.CODIGO_ITEM))
																ELSE
											 						CASE	WHEN RTRIM(MAX(LFSI.CODIGO_ITEM)) = '' OR MAX(LFSI.CODIGO_ITEM) IS NULL THEN
																				RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																			ELSE
																				MAX(LFSI.CODIGO_ITEM) END
															END)), '') + '|',
					DESCR_ITEM		=	ISNULL(RTRIM(LTRIM(MAX(P.DESC_PRODUTO))), MAX(LFSI.DESCRICAO_ITEM)) + '|',
					COD_GEN			=	ISNULL(LEFT(P.CLASSIF_FISCAL, 2), LEFT(LFSI.CLASSIF_FISCAL, 2)) + '|',
					COD_LST			=	'|'
				FROM
					LF_REGISTRO_SAIDA_ITEM AS LFSI
					LEFT JOIN LF_REGISTRO_SAIDA AS LFS ON
						LFSI.NF_SAIDA				=	LFS.NF_SAIDA				AND
						LFSI.SERIE_NF				=	LFS.SERIE_NF				AND
						LFSI.NF_SAIDA_FINAL			=	LFS.NF_SAIDA_FINAL			AND
						LFSI.TAXA_IMPOSTO			=	LFS.TAXA_IMPOSTO			AND
						LFSI.CODIGO_FISCAL_OPERACAO	=	LFS.CODIGO_FISCAL_OPERACAO	AND
						LFSI.ID_IMPOSTO				=	LFS.ID_IMPOSTO				AND
						LFSI.COD_MATRIZ_FISCAL		=	LFS.COD_MATRIZ_FISCAL		AND
						LFSI.COD_FILIAL				=	LFS.COD_FILIAL				AND
						LFSI.ITEM					=	LFS.ITEM
		            LEFT JOIN PRODUTOS P (NOLOCK)
			            ON LFSI.CODIGO_ITEM = P.PRODUTO
					LEFT JOIN SERIES_NF AS S ON
						LFS.SERIE_NF				=	S.SERIE_NF
					LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
						LFSI.COD_FILIAL				=	FILIAL.COD_CLIFOR
				WHERE
					LFSI.SERIE_NF				=		'D1'									AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFSI.ID_IMPOSTO				IN		(1, 0)									AND
					LFSI.CODIGO_FISCAL_OPERACAO	=		'5102'									AND
					LFSI.COD_MATRIZ_FISCAL		=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFSI.NF_SAIDA, LFSI.SERIE_NF, LFSI.ITEM_IMPRESSAO, P.CLASSIF_FISCAL, LFSI.CLASSIF_FISCAL

				UNION ALL

		         SELECT DISTINCT --C605
					REG				=	'|0200|',
		            COD_ITEM		=	ISNULL(RTRIM(LTRIM(A.PRODUTO)), '') + '|',
					DESCR_ITEM		=	ISNULL(RTRIM(LTRIM(MAX(P.DESC_PRODUTO))), '') + '|',
					COD_GEN			=	ISNULL(LEFT(P.CLASSIF_FISCAL, 2), '') + '|',
					COD_LST			=	'|'
		         FROM LOJA_VENDA_PRODUTO A (NOLOCK)
		            JOIN LOJA_VENDA D (NOLOCK)
			            ON A.CODIGO_FILIAL = D.CODIGO_FILIAL
			            AND A.TICKET = D.TICKET
			            AND A.DATA_VENDA = D.DATA_VENDA
		            JOIN LOJA_VENDA_PGTO C (NOLOCK)
			            ON D.CODIGO_FILIAL_PGTO = C.CODIGO_FILIAL
			            AND D.TERMINAL_PGTO = C.TERMINAL
			            AND D.LANCAMENTO_CAIXA = C.LANCAMENTO_CAIXA
		            JOIN LOJAS_VAREJO G (NOLOCK)
			            ON A.CODIGO_FILIAL	= G.CODIGO_FILIAL
		            JOIN FILIAIS M (NOLOCK)
			            ON G.FILIAL	 	= M.FILIAL
		            JOIN CADASTRO_CLI_FOR N (NOLOCK)
			            ON M.MATRIZ_FISCAL		= N.NOME_CLIFOR
		            JOIN PRODUTOS P (NOLOCK)
			            ON A.PRODUTO = P.PRODUTO
					JOIN LOJA_CONTROLE_FISCAL B (NOLOCK)
						ON B.CODIGO_FILIAL		= D.CODIGO_FILIAL
						AND B.TERMINAL			= D.TERMINAL
						AND B.DATA_FISCAL		= D.DATA_VENDA
						AND B.ID_EQUIPAMENTO	= C.ID_EQUIPAMENTO
		         WHERE
		            A.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
		            AND C.DATA		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		            AND D.DATA_HORA_CANCELAMENTO IS NULL
		            AND C.NUMERO_CUPOM_FISCAL IS NOT NULL
		        GROUP BY
		        	A.PRODUTO, P.CLASSIF_FISCAL
			) A
			WHERE
				A.COD_ITEM <> '|' OR
				A.DESCR_ITEM <> '|'
			GROUP BY A.COD_ITEM
			ORDER BY A.COD_ITEM

----------------------------------------------------------------------------------------------------------------------------------------------------
-- 0400
			SELECT
				REG			=	RES.REG,
				COD_NAT		=	RES.COD_NAT,
				DESCR_NAT	=	RES.DESCR_NAT
			FROM (
				SELECT
					REG			=	'|0400|',
					COD_NAT		=	ISNULL(RTRIM(LTRIM(NE.NATUREZA)), RTRIM(LTRIM(NE1.NATUREZA_OPERACAO_CODIGO))) + '|',
					DESCR_NAT	=	ISNULL(RTRIM(LTRIM(NE.DESC_NATUREZA)), RTRIM(LTRIM(NE1.NATUREZA_DESCRICAO))) + '|'
				FROM LF_REGISTRO_ENTRADA LFE
					LEFT JOIN CADASTRO_CLI_FOR C ON
						LFE.COD_CLIFOR				=	C.COD_CLIFOR
					LEFT JOIN ENTRADAS E
						ON	LFE.NF_ENTRADA			= E.NF_ENTRADA			AND
							LFE.SERIE_NF_ENTRADA	= E.SERIE_NF_ENTRADA	AND
							C.NOME_CLIFOR			= E.NOME_CLIFOR
					LEFT JOIN NATUREZAS_ENTRADAS NE
						ON	E.NATUREZA			= NE.NATUREZA
					LEFT JOIN LOJA_NOTA_FISCAL LNF
						ON	LFE.NF_ENTRADA			= LNF.NF_NUMERO			AND
							LFE.SERIE_NF_ENTRADA	= LNF.SERIE_NF			AND
							LFE.COD_CLIFOR			= LNF.CODIGO_CLIENTE
					LEFT JOIN LOJAS_NATUREZA_OPERACAO NE1
						ON	LNF.NATUREZA_OPERACAO_CODIGO	= NE1.NATUREZA_OPERACAO_CODIGO
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA		=	S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
				WHERE
					ES.DESC_ESPECIE_SERIE	<>		'ECF'									AND
					LFE.ID_IMPOSTO			IN		(1, 2, 12)								AND
					LFE.COD_MATRIZ_FISCAL	=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO			BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT 	DISTINCT
					REG			=	'|0400|',
					COD_NAT		=	ISNULL(RTRIM(LTRIM(NS.NATUREZA_SAIDA)), RTRIM(LTRIM(NS1.NATUREZA_OPERACAO_CODIGO))) + '|',
					DESCR_NAT	=	ISNULL(RTRIM(LTRIM(NS.DESC_NATUREZA)), RTRIM(LTRIM(NS1.NATUREZA_DESCRICAO))) + '|'
				FROM LF_REGISTRO_SAIDA LFS
					LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
						LFS.COD_FILIAL			=	FILIAL.COD_CLIFOR
					LEFT JOIN FATURAMENTO F
						ON	FILIAL.NOME_CLIFOR	= F.FILIAL		AND
							LFS.SERIE_NF		= F.SERIE_NF	AND
							LFS.NF_SAIDA		= F.NF_SAIDA
					LEFT JOIN NATUREZAS_SAIDAS NS
						ON	F.NATUREZA_SAIDA	= NS.NATUREZA_SAIDA
					LEFT JOIN LOJA_NOTA_FISCAL LNF
						ON	LFS.COD_FILIAL		= LNF.CODIGO_FILIAL	AND
							LFS.SERIE_NF		= LNF.SERIE_NF		AND
							LFS.NF_SAIDA		= LNF.NF_NUMERO
					LEFT JOIN LOJAS_NATUREZA_OPERACAO NS1
						ON	LNF.NATUREZA_OPERACAO_CODIGO	= NS1.NATUREZA_OPERACAO_CODIGO
				WHERE
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.ID_IMPOSTO				IN		(1, 2, 12)								AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_MATRIZ_FISCAL		=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			) RES
			WHERE
				RES.COD_NAT		<>	'|'	AND
				RES.DESCR_NAT	<>	'|'
			GROUP BY RES.REG, RES.COD_NAT, RES.DESCR_NAT
			ORDER BY RES.REG, RES.COD_NAT, RES.DESCR_NAT

----------------------------------------------------------------------------------------------------------------------------------------------------
-- 0450
	SELECT DISTINCT * FROM (
		SELECT
			REG				=	'|0450|',
			COD_INF_OBS		=	CAST(0 AS NUMERIC(6)),
			TXT				=	ISNULL(RTRIM(LTRIM(MAX(CASE WHEN A.ID_IMPOSTO = 1 THEN A.OBS ELSE '' END))), '') + '|'
		FROM LF_REGISTRO_ENTRADA A
			LEFT JOIN SERIES_NF AS S ON
				A.SERIE_NF_ENTRADA	=	S.SERIE_NF
		WHERE A.ID_IMPOSTO IN (1, 2, 12)
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.RECEBIMENTO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY A.NF_PROPRIA_EMITIDA, A.COD_CLIFOR, A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_ENTRADA, A.EMISSAO,
			A.CTB_LANCAMENTO, A.RECEBIMENTO, A.SERIE_NF_ENTRADA, A.COD_CLIFOR, A.COD_FILIAL

		UNION ALL

		SELECT 	DISTINCT
			REG				=	'|0450|',
			COD_INF_OBS		=	CAST(0 AS NUMERIC(6)),
			TXT				=	ISNULL(RTRIM(LTRIM(MAX(CASE WHEN A.ID_IMPOSTO = 1 THEN A.OBS ELSE '' END))), '') + '|'
		FROM LF_REGISTRO_SAIDA A
			LEFT JOIN SERIES_NF AS S ON
				A.SERIE_NF	=	S.SERIE_NF
		WHERE ID_IMPOSTO IN (1, 2, 12)
			AND A.ESPECIE <> 'ECF'
			AND ESPECIE <> 'CF'
			AND A.SERIE_NF <> 'D1'
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY A.COD_CLIFOR, A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_SAIDA, A.EMISSAO, A.CTB_LANCAMENTO,
				A.DATA_SAIDA, A.SERIE_NF, A.COD_CLIFOR, A.COD_FILIAL

		UNION ALL

		SELECT 	DISTINCT
			REG				=	'|0450|',
			COD_INF_OBS		=	CAST(0 AS NUMERIC(6)),
			TXT				=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|'
		FROM LF_REGISTRO_SAIDA A
			LEFT JOIN SERIES_NF AS S ON
				A.SERIE_NF	=	S.SERIE_NF
		WHERE A.SERIE_NF = 'D1'
			AND ID_IMPOSTO = 1
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY S.COD_SERIE_SINTEGRA, A.NF_SAIDA, A.EMISSAO, A.CTB_LANCAMENTO, A.NOTA_CANCELADA, A.OBS,
			A.SERIE_NF, A.COD_FILIAL

		UNION ALL

		SELECT
			REG				=	'|0450|',
			COD_INF_OBS		=	CAST(0 AS NUMERIC(6)),
			TXT				=	ISNULL(RTRIM(LTRIM(LCF.OBS)), '') + '|'
		FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
			JOIN LOJAS_VAREJO AS FIL (NOLOCK)
				ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
			JOIN FILIAIS (NOLOCK)
				ON FILIAIS.FILIAL = FIL.FILIAL
			LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
				ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 				AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF						AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO IN (14, 22)
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
							ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ISS.TERMINAL
							AND LCF.ECF = LCFT_ISS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							ALIQ_ICMS = LCFT.PER_TARIFA,
							VL_BC_ICMS = SUM(LCFT.TOTAL_TARIFA),
							VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END),
							VL_ISEN = SUM(LCFT.VALOR_ISENTO)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 				AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF						AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO  = 1
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.PER_TARIFA) AS LCFT_ICMS
							ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
							AND LCF.ECF = LCFT_ICMS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
		WHERE (RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
			AND LCF.ECF <> 0
			AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
			AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY LCF.TERMINAL, LCF.ID_EQUIPAMENTO, LCF.CONTADOR_REINICIO_OPERACAO, LCF.DATA_FISCAL,
				LCF.CF_INICIAL, LCF.CF_FINAL, LCF.OBS

		UNION ALL

		SELECT
			REG				=	'|0450|',
			COD_INF_OBS		=	CAST(0 AS NUMERIC(6)),
			TXT				=	ISNULL(RTRIM(LTRIM(LCF.OBS)), '') + '|'
		FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
			JOIN LOJAS_VAREJO AS FIL (NOLOCK)
				ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
			JOIN FILIAIS (NOLOCK)
				ON FILIAIS.FILIAL = FIL.FILIAL
			LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
				ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 				AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF						AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO IN (14, 22)
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
							ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ISS.TERMINAL
							AND LCF.ECF = LCFT_ISS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							ALIQ_ICMS = LCFT.PER_TARIFA,
							VL_BC_ICMS = SUM(LCFT.TOTAL_TARIFA),
							VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END),
							VL_ISEN = SUM(LCFT.VALOR_ISENTO)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 				AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF						AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO  = 1
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.PER_TARIFA) AS LCFT_ICMS
							ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
							AND LCF.ECF = LCFT_ICMS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
		WHERE (RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
			AND LCF.ECF <> 0
			AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
			AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

		UNION ALL

		SELECT
			REG			=	'|0450|',
			COD_INF_OBS	=	CAST(0 AS NUMERIC(6)),
			TXT			=	ISNULL(RTRIM(LTRIM(CTB_IMPOSTO_LANCAMENTO.OBS)), '') + '|'
		FROM CTB_LX_IMPOSTO_TIPO
		JOIN CTB_IMPOSTO_LANCAMENTO
			ON CTB_LX_IMPOSTO_TIPO.ID_IMPOSTO = CTB_IMPOSTO_LANCAMENTO.ID_IMPOSTO
		JOIN W_CTB_LANCAMENTO_ITEM
			ON  W_CTB_LANCAMENTO_ITEM.EMPRESA = CTB_IMPOSTO_LANCAMENTO.EMPRESA
				AND W_CTB_LANCAMENTO_ITEM.LANCAMENTO = CTB_IMPOSTO_LANCAMENTO.LANCAMENTO
				AND W_CTB_LANCAMENTO_ITEM.ITEM = CTB_IMPOSTO_LANCAMENTO.ITEM
		JOIN LF_SUB_ITEM_APURACAO
			ON CTB_IMPOSTO_LANCAMENTO.ID_SUB_ITEM_APURACAO = LF_SUB_ITEM_APURACAO.ID_SUB_ITEM_APURACAO
		LEFT JOIN UNIDADES_FEDERACAO
			ON LF_SUB_ITEM_APURACAO.UF = UNIDADES_FEDERACAO.UF
		JOIN LF_LX_CODIGO_APURACAO
			ON LF_LX_CODIGO_APURACAO.LX_CODIGO_APURACAO = LF_SUB_ITEM_APURACAO.LX_CODIGO_APURACAO
		WHERE
			CTB_IMPOSTO_LANCAMENTO.ID_IMPOSTO = 1
			AND W_CTB_LANCAMENTO_ITEM.COD_FILIAL = '<<Alltrim(.PX_COD_CLIFOR)>>'
			AND W_CTB_LANCAMENTO_ITEM.DATA_LANCAMENTO BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		) A
		WHERE A.TXT <> '|'

----------------------------------------------------------------------------------------------------------------------------------------------------
-- B040
         SELECT
         	REG				=	'|B040|',
         	COD_MOD			=	'2D|',
         	ECF_CX			=	ISNULL(RTRIM(LTRIM(A.TERMINAL)), '') + '|',
         	ECF_FAB			=	ISNULL(RTRIM(LTRIM(A.ID_EQUIPAMENTO)), '') + '|',
         	CRO				=	ISNULL(RTRIM(LTRIM(STR(A.CONTADOR_REINICIO_OPERACAO))), '') + '|',
         	CRZ				=	ISNULL(RTRIM(LTRIM(STR(A.CF_FINAL))), '') + '|',
         	NUM_DOC_INI		=	ISNULL(RTRIM(LTRIM(STR(A.CF_INICIAL))), '') + '|',
         	NUM_DOC_FIN		=	ISNULL(RTRIM(LTRIM(STR(A.CF_FINAL))), '') + '|',
         	DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_FISCAL, 103), '/', ''))), '') + '|',
         	GT_INI			=	ISNULL(RTRIM(LTRIM(STR(A.GT_INICIAL, 15, 2))), '') + '|',
         	GT_FIN			=	ISNULL(RTRIM(LTRIM(STR(A.GT_FINAL, 15, 2))), '') + '|',
         	VL_BRT			=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_BRUTO, 15, 2))), '') + '|',
         	VL_CANC_ISSQN	=	'|',
         	VL_CANC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_CANCELADO, 15, 2))), '') + '|',
         	VL_CANC			=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_CANCELADO, 15, 2))), '') + '|',
         	VL_DESC_ISSQN	=	'|',
         	VL_DESC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_DESCONTO, 15, 2))), '') + '|',
         	VL_DESC			=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_DESCONTO, 15, 2))), '') + '|',
         	VL_ACMO_ISSQN	=	'|',
         	VL_ACMO_ICMS	=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_ACRESCIMO, 15, 2))), '') + '|',
         	VL_ACMO			=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_ACRESCIMO, 15, 2))), '') + '|',
         	VL_BC_ISSQN		=	'|',
         	VL_ISSQN		=	'|',
         	VL_ISEN_ISSQN	=	'|',
         	VL_NT_ISSQN		=	'|',
         	VL_RT_ISSQN		=	'|',
         	VL_LIQ			=	ISNULL(RTRIM(LTRIM(STR(A.TOTAL_BRUTO - A.TOTAL_DESCONTO - A.TOTAL_CANCELADO - A.TOTAL_ISENTO - TOTAL_NAO_TRIBUTAVEL, 15, 2))), '') + '|',
         	COD_INF_OBS		=	'|'
         FROM
         	LOJA_CONTROLE_FISCAL A
		WHERE
			A.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.DATA_FISCAL	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C020
			SELECT
				REG			=	'|C020|',
				IND_OPER	=	'0|',
				IND_EMIT	=	CASE WHEN A.NF_PROPRIA_EMITIDA = 0 THEN '1' ELSE '0' END + '|',
				COD_PART	=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				COD_MOD		=	'01|',
				COD_SIT		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER			=	ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF_ENTRADA))) + '|',
				NUM_DOC		=	ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
				DT_DOC		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				DT_E_S		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.RECEBIMENTO, 103), '/', ''))), '') + '|',
				COD_NAT		=	ISNULL(RTRIM(LTRIM(NE.NATUREZA)), RTRIM(LTRIM(NE1.NATUREZA_OPERACAO_CODIGO))) + '|',
				VL_DOC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				IND_PGTO	=	'0|',
				VL_DESC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.DESCONTO ELSE 0 END), 15, 2))), '') + '|',
				VL_MERC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				IND_FRT		=	ISNULL(RTRIM(LTRIM(STR(A.FRETE_A_PAGAR))), '') + '|',
				VL_FRT		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.FRETE ELSE 0 END), 15, 2))), '') + '|',
				VL_SEG		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.SEGURO ELSE 0 END), 15, 2))), '') + '|',
				VL_OUT_DA	=	ISNULL(RTRIM(LTRIM(STR(0, 15, 2))), '') + '|',
				VL_BC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_ST	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 12 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ST		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 12 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				COD_INF_OBS	=	ISNULL(RTRIM(LTRIM(MAX(CASE WHEN A.ID_IMPOSTO = 1 THEN A.OBS ELSE '' END))), '') + '|',
				CLI_FILIAL	=	ISNULL(A.CLIENTE_VAREJO, A.NOME_CLIFOR),
				SERIE		=	A.SERIE_NF_ENTRADA
			FROM W_LF_REGISTRO_ENTRADA_IMPOSTO A
			LEFT JOIN ENTRADAS B
				ON	A.NF_ENTRADA		= B.NF_ENTRADA			AND
					A.SERIE_NF_ENTRADA	= B.SERIE_NF_ENTRADA	AND
					A.NOME_CLIFOR		= B.NOME_CLIFOR
			LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
				ES.ESPECIE_SERIE			=	A.ESPECIE_SERIE
			LEFT JOIN NATUREZAS_ENTRADAS NE
				ON	B.NATUREZA			= NE.NATUREZA
			LEFT JOIN LOJA_NOTA_FISCAL LNF
				ON	A.NF_ENTRADA		= LNF.NF_NUMERO			AND
					A.SERIE_NF_ENTRADA	= LNF.SERIE_NF			AND
					(A.CODIGO_CLIENTE	= LNF.CODIGO_CLIENTE	OR
					A.COD_CLIFOR		= LNF.COD_CLIFOR)
			LEFT JOIN LOJAS_NATUREZA_OPERACAO NE1
				ON	LNF.NATUREZA_OPERACAO_CODIGO	= NE1.NATUREZA_OPERACAO_CODIGO
			WHERE ES.DESC_ESPECIE_SERIE <> 'ECF'
				AND A.CODIGO_FISCAL_OPERACAO NOT IN ('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
													'1256', '2256', '1257', '2257',
													'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
													'1306', '2306',
													'1350', '1352', '1353', '1354', '1355', '1356', '1932',
													'2350', '2352', '2353', '2354', '2355', '2356', '2932',
													'3350', '3352', '3353', '3354', '3355', '3356',
													'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
													'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
													'1932', '2932')
				AND	A.ID_IMPOSTO IN (1, 2, 12)
				AND A.CODIGO_FISCAL_OPERACAO < '9000'
				AND A.COD_MATRIZ_FISCAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.RECEBIMENTO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				CASE WHEN A.NF_PROPRIA_EMITIDA = 0 THEN '1' ELSE '0' END + '|',
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF_ENTRADA))) + '|',
				ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.RECEBIMENTO, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(NE.NATUREZA)), RTRIM(LTRIM(NE1.NATUREZA_OPERACAO_CODIGO))) + '|',
				ISNULL(RTRIM(LTRIM(STR(A.FRETE_A_PAGAR))), '') + '|',
				ISNULL(A.CLIENTE_VAREJO, A.NOME_CLIFOR),
				A.SERIE_NF_ENTRADA

			UNION ALL

			SELECT 	DISTINCT
				REG			=	'|C020|',
				IND_OPER	=	'1|',
				IND_EMIT	=	'0|',
				COD_PART	=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				COD_MOD		=	'01|',
				COD_SIT		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER			=	ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF))) + '|',
				NUM_DOC		=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				DT_DOC		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				DT_E_S		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_SAIDA, 103), '/', ''))), '') + '|',
				COD_NAT		=	ISNULL(RTRIM(LTRIM(B.NATUREZA_SAIDA)), RTRIM(LTRIM(LNF.NATUREZA_OPERACAO_CODIGO))) + '|',
				VL_DOC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				IND_PGTO	=	'0|',
				VL_DESC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.DESCONTO ELSE 0 END), 15, 2))), '') + '|',
				VL_MERC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				IND_FRT		=	ISNULL(RTRIM(LTRIM(STR(0))), '') + '|',
				VL_FRT		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.FRETE ELSE 0 END), 15, 2))), '') + '|',
				VL_SEG		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.SEGURO ELSE 0 END), 15, 2))), '') + '|',
				VL_OUT_DA	=	ISNULL(RTRIM(LTRIM(STR(0, 15, 2))), '') + '|',
				VL_BC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_ST	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 12 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ST		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 12 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				COD_INF_OBS	=	ISNULL(RTRIM(LTRIM(MAX(CASE WHEN A.ID_IMPOSTO = 1 THEN A.OBS ELSE '' END))), '') + '|',
				CLI_FILIAL	=	ISNULL(A.CLIENTE_VAREJO, A.NOME_CLIFOR),
				SERIE		=	A.SERIE_NF
			FROM W_LF_REGISTRO_SAIDA_IMPOSTO A
			LEFT JOIN FATURAMENTO B
				ON	A.FILIAL			= B.FILIAL		AND
					A.SERIE_NF			= B.SERIE_NF	AND
					A.NF_SAIDA			= B.NF_SAIDA
			LEFT JOIN LOJAS_VAREJO AS LV
				ON	LV.FILIAL = A.FILIAL
			LEFT JOIN LOJA_NOTA_FISCAL LNF
				ON	LV.CODIGO_FILIAL	= LNF.CODIGO_FILIAL	AND
					A.SERIE_NF			= LNF.SERIE_NF		AND
					A.NF_SAIDA			= LNF.NF_NUMERO
			LEFT JOIN LOJAS_NATUREZA_OPERACAO NS1
				ON	LNF.NATUREZA_OPERACAO_CODIGO	= NS1.NATUREZA_OPERACAO_CODIGO
			WHERE A.ESPECIE NOT IN ('ECF', 'CF') AND A.ID_IMPOSTO IN (1, 2, 12) AND A.SERIE_NF <> 'D1'
				AND A.COD_MATRIZ_FISCAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF))) + '|',
				ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_SAIDA, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(B.NATUREZA_SAIDA)), RTRIM(LTRIM(LNF.NATUREZA_OPERACAO_CODIGO))) + '|',
				ISNULL(A.CLIENTE_VAREJO, A.NOME_CLIFOR),
				A.SERIE_NF

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C300
			SELECT
				REG				=	'|C300|',
				COD_ITEM		=	ISNULL(RTRIM(LTRIM(CASE	WHEN MAX(LFEI.COD_TABELA_FILHA) IN ('C', 'A', NULL) THEN
																RTRIM(LFEI.NF_ENTRADA) +
																RTRIM(CAST(CAST(LFEI.ITEM_IMPRESSAO AS INT) AS VARCHAR(3)))+
																RTRIM(CASE WHEN ISNUMERIC(MAX(LFEI.COD_CLIFOR)) = 1 THEN CAST(CAST(MAX(LFEI.COD_CLIFOR) AS INT) AS VARCHAR(6)) ELSE CAST(MAX(LFEI.COD_CLIFOR) AS VARCHAR(6)) END)
															WHEN MAX(LFEI.COD_TABELA_FILHA) IN ('P', 'R') THEN
																'P'+RTRIM(MAX(LFEI.CODIGO_ITEM))
															WHEN MAX(LFEI.COD_TABELA_FILHA) IN ('M', 'T') THEN
																'M'+RTRIM(MAX(LFEI.CODIGO_ITEM))
															ELSE
																CASE	WHEN RTRIM(MAX(LFEI.CODIGO_ITEM)) = '' OR MAX(LFEI.CODIGO_ITEM) IS NULL THEN
																			RTRIM(LFEI.NF_ENTRADA) +
																			RTRIM(CAST(CAST(LFEI.ITEM_IMPRESSAO AS INT) AS VARCHAR(3)))+
																			RTRIM(CASE WHEN ISNUMERIC(MAX(LFEI.COD_CLIFOR)) = 1 THEN CAST(CAST(MAX(LFEI.COD_CLIFOR) AS INT) AS VARCHAR(6)) ELSE CAST(MAX(LFEI.COD_CLIFOR) AS VARCHAR(6)) END)
																		ELSE
																			MAX(LFEI.CODIGO_ITEM)
																END
														END)), '') + '|',
				VL_UNIT			=	ISNULL(RTRIM(LTRIM(STR(SUM(LFEI.PRECO_UNITARIO), 15, 3))), '0') + '|',
				QTD				=	ISNULL(RTRIM(LTRIM(STR(SUM(LFEI.QTDE_ITEM), 15, 3))), '0') + '|',
				UNID			=	ISNULL(RTRIM(LTRIM(LFEI.UNIDADE)), '') + '|',
				VL_ITEM			=	ISNULL(RTRIM(LTRIM(STR(SUM(LFEI.QTDE_ITEM * LFEI.PRECO_UNITARIO), 15, 2))), '0') + '|',
				VL_DESC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN ISNULL(LFEI.VALOR_DESCONTOS, 0) < 0 THEN 0 ELSE ISNULL(LFEI.VALOR_DESCONTOS, 0) END), 15, 2))), '0') + '|',
				IND_MOV			=	'0|',
				COD_NCM			=	ISNULL(LEFT(RTRIM(LTRIM(LFEI.CLASSIF_FISCAL)), 8), '0') + '|',
				EX_NCM			=	'|',
				EX_II			=	'|',
				CST				=	ISNULL(RTRIM(LTRIM(LFEI.TRIBUT_ORIGEM)) + RTRIM(LTRIM(LFEI.TRIBUT_ICMS)), '000') + '|',
				CFOP			=	ISNULL(RTRIM(LTRIM(LFEI.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_BC_ICMS_I	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFEI.ID_IMPOSTO = 1  THEN LFEI.BASE_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(MAX(CASE WHEN LFEI.ID_IMPOSTO = 1  THEN LFEI.TAXA_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_ICMS_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFEI.ID_IMPOSTO = 1  THEN LFEI.VALOR_IMPOSTO ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_BC_ST_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFEI.ID_IMPOSTO = 12 THEN LFEI.BASE_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				ALIQ_ST			=	ISNULL(RTRIM(LTRIM(STR(MAX(CASE WHEN LFEI.ID_IMPOSTO = 12 THEN LFEI.TAXA_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_ST_I			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFEI.ID_IMPOSTO = 12 THEN LFEI.VALOR_IMPOSTO ELSE 0.00 END), 15, 2))), '0') + '|',
				CTIPI			=	ISNULL(RTRIM(LTRIM('00')), '') + '|',
				VL_BC_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFEI.ID_IMPOSTO = 2  THEN LFEI.BASE_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				ALIQ_IPI		=	ISNULL(RTRIM(LTRIM(STR(MAX(CASE WHEN LFEI.ID_IMPOSTO = 2  THEN LFEI.TAXA_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_IPI_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFEI.ID_IMPOSTO = 2  THEN LFEI.VALOR_IMPOSTO ELSE 0.00 END), 15, 2))), '0') + '|',
				SERIE_OFICIAL	=	ISNULL(S.COD_SERIE_SINTEGRA, LFEI.SERIE_NF_ENTRADA),
				CLI_FILIAL		=	ISNULL(LFEI.CLIENTE_VAREJO, LFEI.NOME_CLIFOR),
				SERIE			=	LFEI.SERIE_NF_ENTRADA,
				NUM_DOC			=	LFEI.NF_ENTRADA
			FROM
				W_LF_REGISTRO_ENTRADA_IMPOSTO_ITEM AS LFEI
	            LEFT JOIN PRODUTOS P (NOLOCK)
		            ON LFEI.CODIGO_ITEM = P.PRODUTO
				LEFT JOIN SERIES_NF AS S ON
					LFEI.SERIE_NF_ENTRADA		=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
			WHERE
				ES.DESC_ESPECIE_SERIE		<>		'ECF'									AND
				LFEI.ID_IMPOSTO				IN		(1, 2, 12)								AND
				LFEI.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
													'1256', '2256', '1257', '2257',
													'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
													'1306', '2306',
													'1350', '1352', '1353', '1354', '1355', '1356', '1932',
													'2350', '2352', '2353', '2354', '2355', '2356', '2932',
													'3350', '3352', '3353', '3354', '3355', '3356',
													'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
													'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
													'1932', '2932')							AND
				LFEI.CODIGO_FISCAL_OPERACAO	<		'9000'									AND
				LFEI.COD_MATRIZ_FISCAL		=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
				LFEI.RECEBIMENTO			BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				LFEI.NF_ENTRADA, LFEI.ITEM_IMPRESSAO, LFEI.UNIDADE, LFEI.CLASSIF_FISCAL, LFEI.TRIBUT_ORIGEM,
				LFEI.TRIBUT_ICMS, LFEI.CODIGO_FISCAL_OPERACAO, S.COD_SERIE_SINTEGRA, LFEI.SERIE_NF_ENTRADA,
				ISNULL(LFEI.CLIENTE_VAREJO, LFEI.NOME_CLIFOR)

			UNION ALL

			SELECT
				REG				=	'|C300|',
				COD_ITEM		=	ISNULL(RTRIM(LTRIM(CASE WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('C', 'A', NULL) THEN
																RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
															WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('P', 'R') THEN
																'P'+RTRIM(MAX(LFSI.CODIGO_ITEM))
															WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('M', 'T') THEN
																'M'+RTRIM(MAX(LFSI.CODIGO_ITEM))
															ELSE
											 					CASE	WHEN RTRIM(MAX(LFSI.CODIGO_ITEM)) = '' OR MAX(LFSI.CODIGO_ITEM) IS NULL THEN
																			RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																		ELSE
																			MAX(LFSI.CODIGO_ITEM) END
														END)), '') + '|',
				VL_UNIT			=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.PRECO_UNITARIO), 15, 3))), '0') + '|',
				QTD				=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.QTDE_ITEM), 15, 3))), '0') + '|',
				UNID			=	ISNULL(RTRIM(LTRIM(LFSI.UNIDADE)), '') + '|',
				VL_ITEM			=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.QTDE_ITEM * LFSI.PRECO_UNITARIO), 15, 2))), '0') + '|',
				VL_DESC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN ISNULL(LFSI.DESCONTO, 0) < 0 THEN 0 ELSE ISNULL(LFSI.DESCONTO, 0) END), 15, 2))), '0') + '|',
				IND_MOV			=	'0|',
				COD_NCM			=	ISNULL(LEFT(RTRIM(LTRIM(LFSI.CLASSIF_FISCAL)), 8), '0') + '|',
				EX_NCM			=	'|',
				EX_II			=	'|',
				CST				=	ISNULL(RTRIM(LTRIM(LFSI.TRIBUT_ORIGEM)) + RTRIM(LTRIM(LFSI.TRIBUT_ICMS)), '000') + '|',
				CFOP			=	ISNULL(RTRIM(LTRIM(LFSI.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_BC_ICMS_I	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFSI.ID_IMPOSTO = 1  THEN LFSI.BASE_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(MAX(CASE WHEN LFSI.ID_IMPOSTO = 1  THEN LFSI.TAXA_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_ICMS_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFSI.ID_IMPOSTO = 1  THEN LFSI.VALOR_IMPOSTO ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_BC_ST_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFSI.ID_IMPOSTO = 12 THEN LFSI.BASE_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				ALIQ_ST			=	ISNULL(RTRIM(LTRIM(STR(MAX(CASE WHEN LFSI.ID_IMPOSTO = 12 THEN LFSI.TAXA_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_ST_I			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFSI.ID_IMPOSTO = 12 THEN LFSI.VALOR_IMPOSTO ELSE 0.00 END), 15, 2))), '0') + '|',
				CTIPI			=	ISNULL(RTRIM(LTRIM('00')), '') + '|',
				VL_BC_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFSI.ID_IMPOSTO = 2  THEN LFSI.BASE_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				ALIQ_IPI		=	ISNULL(RTRIM(LTRIM(STR(MAX(CASE WHEN LFSI.ID_IMPOSTO = 2  THEN LFSI.TAXA_IMPOSTO  ELSE 0.00 END), 15, 2))), '0') + '|',
				VL_IPI_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN LFSI.ID_IMPOSTO = 2  THEN LFSI.VALOR_IMPOSTO ELSE 0.00 END), 15, 2))), '0') + '|',
				SERIE_OFICIAL	=	ISNULL(S.COD_SERIE_SINTEGRA, LFSI.SERIE_NF),
				CLI_FILIAL		=	ISNULL(LFSI.CLIENTE_VAREJO, LFSI.NOME_CLIFOR),
				SERIE			=	LFSI.SERIE_NF,
				NUM_DOC			=	LFSI.NF_SAIDA
			FROM
				W_LF_REGISTRO_SAIDA_IMPOSTO_ITEM AS LFSI
	            LEFT JOIN PRODUTOS P (NOLOCK)
		            ON LFSI.CODIGO_ITEM = P.PRODUTO
				LEFT JOIN SERIES_NF AS S ON
					LFSI.SERIE_NF		=	S.SERIE_NF
			WHERE
				LFSI.ESPECIE			NOT IN	('ECF', 'CF')							AND
				LFSI.ID_IMPOSTO			IN		(1, 2, 12)								AND
				LFSI.SERIE_NF			<>		'D1'									AND
				LFSI.COD_FILIAL			=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
				LFSI.EMISSAO			BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				LFSI.NF_SAIDA, LFSI.ITEM_IMPRESSAO, LFSI.UNIDADE, LFSI.CLASSIF_FISCAL, LFSI.TRIBUT_ORIGEM,
				LFSI.TRIBUT_ICMS, LFSI.CODIGO_FISCAL_OPERACAO, S.COD_SERIE_SINTEGRA, LFSI.SERIE_NF, ISNULL(LFSI.CLIENTE_VAREJO, LFSI.NOME_CLIFOR)

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C550
			SELECT 	DISTINCT
				REG			=	'|C550|',
				COD_MOD		=	'02|',
				COD_SIT		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER			=	ISNULL(RTRIM(LTRIM(S.COD_SERIE_SINTEGRA)), '') + '|',
				SUB			=	'|',
				NUM_DOC		=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				DT_DOC		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				VL_DOC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				VL_DESC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.DESCONTO ELSE 0 END), 15, 2))), '') + '|',
				VL_MERC		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				COD_INF_OBS	=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				FILIAL		=	FILIAL.NOME_CLIFOR,
				A.SERIE_NF,
				A.CODIGO_FISCAL_OPERACAO
			FROM LF_REGISTRO_SAIDA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF				=	S.SERIE_NF
				LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
					A.COD_FILIAL		=	FILIAL.COD_CLIFOR
			WHERE A.SERIE_NF = 'D1' AND A.ESPECIE NOT IN ('ECF', 'CF') AND ID_IMPOSTO IN (1, 0)
				AND A.COD_MATRIZ_FISCAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_SAIDA, A.EMISSAO, A.OBS, FILIAL.NOME_CLIFOR, A.SERIE_NF, A.CODIGO_FISCAL_OPERACAO

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C555
			SELECT
				REG					=	'|C555|',
				COD_ITEM			=	ISNULL(RTRIM(LTRIM(CASE WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('C', 'A', NULL) THEN
																	RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('P', 'R') THEN
																	'P'+RTRIM(MAX(LFSI.CODIGO_ITEM))
																WHEN MAX(LFSI.COD_TABELA_FILHA) IN ('M', 'T') THEN
																	'M'+RTRIM(MAX(LFSI.CODIGO_ITEM))
																ELSE
											 						CASE	WHEN RTRIM(MAX(LFSI.CODIGO_ITEM)) = '' OR MAX(LFSI.CODIGO_ITEM) IS NULL THEN
																				RTRIM(LFSI.NF_SAIDA)+RTRIM(LFSI.SERIE_NF)+RTRIM(LFSI.ITEM_IMPRESSAO)
																			ELSE
																				MAX(LFSI.CODIGO_ITEM) END
															END)), '') + '|',
				VL_UNIT				=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.PRECO_UNITARIO), 15, 3))), '') + '|',
				QTD					=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.QTDE_ITEM), 15, 3))), '') + '|',
				UNID				=	ISNULL(RTRIM(LTRIM(LFSI.UNIDADE)), '') + '|',
				VL_ITEM				=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.QTDE_ITEM * LFSI.PRECO_UNITARIO), 15, 2))), '') + '|',
				VL_DESC_I			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN ISNULL(LFSI.DESCONTO, 0) < 0 THEN 0 ELSE ISNULL(LFSI.DESCONTO, 0) END), 15, 2))), '') + '|',
				CST					=	ISNULL(RTRIM(LTRIM(LFSI.TRIBUT_ORIGEM)) + RTRIM(LTRIM(LFSI.TRIBUT_ICMS)), '') + '|',
				CFOP				=	ISNULL(RTRIM(LTRIM(LFSI.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_BC_ICMS_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.BASE_IMPOSTO), 15, 2))), '') + '|',
				ALIQ_ICMS			=	ISNULL(RTRIM(LTRIM(STR(MAX(LFSI.TAXA_IMPOSTO), 15, 2))), '') + '|',
				VL_ICMS_I			=	ISNULL(RTRIM(LTRIM(STR(SUM(LFSI.VALOR_IMPOSTO), 15, 2))), '') + '|',
				SERIE_NF_OFICIAL	=	ISNULL(S.COD_SERIE_SINTEGRA, LFSI.SERIE_NF),
				NF_SAIDA			=	LFSI.NF_SAIDA,
				FILIAL				=	FILIAL.NOME_CLIFOR,
				SERIE_NF			=	LFSI.SERIE_NF
			FROM
				LF_REGISTRO_SAIDA_ITEM AS LFSI
				LEFT JOIN LF_REGISTRO_SAIDA AS LFS ON
					LFSI.NF_SAIDA				=	LFS.NF_SAIDA				AND
					LFSI.SERIE_NF				=	LFS.SERIE_NF				AND
					LFSI.NF_SAIDA_FINAL			=	LFS.NF_SAIDA_FINAL			AND
					LFSI.TAXA_IMPOSTO			=	LFS.TAXA_IMPOSTO			AND
					LFSI.CODIGO_FISCAL_OPERACAO	=	LFS.CODIGO_FISCAL_OPERACAO	AND
					LFSI.ID_IMPOSTO				=	LFS.ID_IMPOSTO				AND
					LFSI.COD_MATRIZ_FISCAL		=	LFS.COD_MATRIZ_FISCAL		AND
					LFSI.COD_FILIAL				=	LFS.COD_FILIAL				AND
					LFSI.ITEM					=	LFS.ITEM
	            LEFT JOIN PRODUTOS P (NOLOCK)
		            ON LFSI.CODIGO_ITEM = P.PRODUTO
				LEFT JOIN SERIES_NF AS S ON
					LFS.SERIE_NF				=	S.SERIE_NF
				LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
					LFSI.COD_FILIAL				=	FILIAL.COD_CLIFOR
			WHERE
				LFSI.SERIE_NF				=		'D1'									AND
				LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
				LFSI.ID_IMPOSTO				IN		(1, 0)									AND
				LFSI.CODIGO_FISCAL_OPERACAO	=		'5102'									AND
				LFSI.COD_MATRIZ_FISCAL		=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
				LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				LFSI.NF_SAIDA, LFSI.SERIE_NF, LFSI.ITEM_IMPRESSAO, LFSI.UNIDADE, LFSI.TRIBUT_ORIGEM,
				LFSI.TRIBUT_ICMS, LFSI.CODIGO_FISCAL_OPERACAO, S.COD_SERIE_SINTEGRA, LFSI.SERIE_NF, FILIAL.NOME_CLIFOR

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C600
				SELECT
					REG				=	'|C600|',
					COD_MOD			=	'2D|',
					COD_SIT			=	CASE WHEN LV.VALOR_CANCELADO > 0 THEN '02|' ELSE '00|' END,
					ECF_CX			=	ISNULL(RTRIM(LTRIM(LCF.TERMINAL)), '') + '|',
					ECF_FAB			=	ISNULL(RTRIM(LTRIM(LCF.ID_EQUIPAMENTO)), '') + '|',
					CRO				=	ISNULL(RTRIM(LTRIM(STR(LCF.CONTADOR_REINICIO_OPERACAO))), '')	+	'|',
					CRZ				=	ISNULL(RTRIM(LTRIM(STR(LCF.QTDE_REDUCOES_Z))), '')	+	'|',
					NUM_DOC			=	ISNULL(RTRIM(LTRIM(LVP.NUMERO_CUPOM_FISCAL)), '')		+	'|',
					DT_DOC			=	LV.DATA_VENDA,
					VL_DOC			=	ISNULL(RTRIM(LTRIM(STR(LV.VALOR_VENDA_BRUTA, 15, 2))), '') + '|',
					VL_CANC_ISSQN	=	'|',
					VL_CANC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(LV.VALOR_CANCELADO, 15, 2))), '') + '|',
					VL_CANC			=	ISNULL(RTRIM(LTRIM(STR(LV.VALOR_CANCELADO, 15, 2))), '') + '|',
					VL_DESC_ISSQN	=	'|',
					VL_DESC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN LV.DESCONTO > 0 THEN LV.DESCONTO ELSE 0.00 END, 15, 2))), '') + '|',
					VL_DESC			=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN LV.DESCONTO > 0 THEN LV.DESCONTO ELSE 0.00 END, 15, 2))), '') + '|',
					VL_ACMO_ISSQN	=	'|',
					VL_ACMO_ICMS	=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN LV.DESCONTO < 0 THEN ABS(LV.DESCONTO) ELSE 0.00 END, 15, 2))), '') + '|',
					VL_ACMO			=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN LV.DESCONTO < 0 THEN ABS(LV.DESCONTO) ELSE 0.00 END, 15, 2))), '') + '|',
					VL_ISSQN		=	'|',
					VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(LV.VALOR_VENDA_BRUTA, 15, 2))), '') + '|',
					VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN LV.VALOR_VENDA_BRUTA = 0 THEN 0.00 ELSE (LV.VALOR_VENDA_BRUTA * ISNULL(UFI.ICMS_SAIDA, 0.00)) / 100 END, 15, 2))), '') + '|',
					VL_ISEN			=	'|',
					VL_NT			=	'|',
					VL_ST			=	'|',
					LV.CODIGO_FILIAL,
					LV.TICKET,
					LV.DATA_VENDA
				FROM LOJA_VENDA LV
					JOIN LOJA_VENDA_PGTO LVP ON
					  LV.CODIGO_FILIAL_PGTO	= LVP.CODIGO_FILIAL	AND
					  LV.TERMINAL_PGTO		= LVP.TERMINAL		AND
					  LV.LANCAMENTO_CAIXA	= LVP.LANCAMENTO_CAIXA
					JOIN LOJA_CONTROLE_FISCAL LCF ON
					  LCF.CODIGO_FILIAL		= LV.CODIGO_FILIAL	AND
					  LCF.TERMINAL			= LV.TERMINAL		AND
					  LCF.DATA_FISCAL		= LV.DATA_VENDA		AND
					  LCF.ID_EQUIPAMENTO	= LVP.ID_EQUIPAMENTO
					JOIN LOJAS_VAREJO L ON
					  LVP.CODIGO_FILIAL		= L.CODIGO_FILIAL
					JOIN FILIAIS F (NOLOCK) ON
					  L.FILIAL	 			= F.FILIAL
					JOIN CADASTRO_CLI_FOR CF (NOLOCK) ON
					  F.MATRIZ_FISCAL		= CF.NOME_CLIFOR
					JOIN UNIDADES_FEDERACAO_ICMS UFI ON
					  CF.UF 				= UFI.UF			AND
					  CF.UF			 		= UFI.UF_DESTINO
				WHERE
					LV.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
					AND LVP.DATA		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					AND LV.DATA_HORA_CANCELAMENTO IS NULL
					AND LVP.NUMERO_CUPOM_FISCAL IS NOT NULL
				ORDER BY LCF.TERMINAL, LCF.ID_EQUIPAMENTO, LCF.CONTADOR_REINICIO_OPERACAO, LCF.QTDE_REDUCOES_Z, LCF.CF_INICIAL, LCF.CF_FINAL, LCF.DATA_FISCAL

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C605
		         SELECT
		            REG				=	'|C605|',
		            NUM_ITEM		=	ISNULL(RTRIM(LTRIM(A.ITEM)), '0') + '|',
		            COD_ITEM		=	ISNULL(RTRIM(LTRIM(A.PRODUTO)), '') + '|',
		            VL_UNIT			=	ISNULL(RTRIM(LTRIM(STR((A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 15, 3))), '0') + '|',
		            QTD				=	ISNULL(RTRIM(LTRIM(STR(A.QTDE, 15, 3))), '0') + '|',
		            QTD_CANC_I		=	ISNULL(RTRIM(LTRIM(STR(A.QTDE_CANCELADA, 15, 3))), '0') + '|',
		            UNID			=	ISNULL(RTRIM(LTRIM(P.UNIDADE)), '') + '|',
		            VL_ITEM			=	ISNULL(RTRIM(LTRIM(STR((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 15, 2))), '0') + '|',
		            VL_DESC_I		=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN A.DESCONTO_ITEM > 0 THEN A.DESCONTO_ITEM ELSE 0.00 END, 15, 2))), '0') + '|',
		            VL_CANC_I		=	ISNULL(RTRIM(LTRIM(STR(C.VALOR_CANCELADO, 15, 2))), '0') + '|',
		            VL_ACMO_I		=	ISNULL(RTRIM(LTRIM(STR(CASE WHEN A.DESCONTO_ITEM < 0 THEN ABS(A.DESCONTO_ITEM) ELSE 0.00 END, 15, 2))), '0') + '|',
		            VL_ISSQN_I		=	'0|',
		            CST				=	ISNULL(RTRIM(LTRIM(P.TRIBUT_ORIGEM)) + RTRIM(LTRIM(P.TRIBUT_ICMS)), '000') + '|',
					CFOP			=	ISNULL(RTRIM(LTRIM(B.CODIGO_FISCAL_OPERACAO)), '5102') + '|',
		            VL_BC_ICMS_I	=	ISNULL(RTRIM(LTRIM(STR((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 15, 2))), '0') + '|',
		            ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(A.ALIQUOTA, 6, 2))), '0') + '|',
		            VL_ICMS_I		=	ISNULL(RTRIM(LTRIM(STR(((ISNULL((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 0) * ISNULL(A.ALIQUOTA, 0)) / 100), 15, 2))), '0') + '|',
		            VL_ISEN_I		=	'0|',
		            VL_NT_I			=	'0|',
		            VL_ST_I			=	'0|',
					A.CODIGO_FILIAL,
					A.TICKET,
					A.DATA_VENDA
		         FROM LOJA_VENDA_PRODUTO A (NOLOCK)
		            JOIN LOJA_VENDA D (NOLOCK)
			            ON A.CODIGO_FILIAL = D.CODIGO_FILIAL
			            AND A.TICKET = D.TICKET
			            AND A.DATA_VENDA = D.DATA_VENDA
		            JOIN LOJA_VENDA_PGTO C (NOLOCK)
			            ON D.CODIGO_FILIAL_PGTO = C.CODIGO_FILIAL
			            AND D.TERMINAL_PGTO = C.TERMINAL
			            AND D.LANCAMENTO_CAIXA = C.LANCAMENTO_CAIXA
		            JOIN LOJAS_VAREJO G (NOLOCK)
			            ON A.CODIGO_FILIAL	= G.CODIGO_FILIAL
		            JOIN FILIAIS M (NOLOCK)
			            ON G.FILIAL	 	= M.FILIAL
		            JOIN CADASTRO_CLI_FOR N (NOLOCK)
			            ON M.MATRIZ_FISCAL		= N.NOME_CLIFOR
		            JOIN PRODUTOS P (NOLOCK)
			            ON A.PRODUTO = P.PRODUTO
					JOIN LOJA_CONTROLE_FISCAL B (NOLOCK)
						ON B.CODIGO_FILIAL		= D.CODIGO_FILIAL
						AND B.TERMINAL			= D.TERMINAL
						AND B.DATA_FISCAL		= D.DATA_VENDA
						AND B.ID_EQUIPAMENTO	= C.ID_EQUIPAMENTO
		         WHERE
		            A.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
		            AND C.DATA		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		            AND D.DATA_HORA_CANCELAMENTO IS NULL
		            AND C.NUMERO_CUPOM_FISCAL IS NOT NULL

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C620
		    SELECT
				REG				=	'|C620|',
		        COD_MOD			=	'2D|',
		        ECF_CX			=	ISNULL(RTRIM(LTRIM(LCF.TERMINAL)), '0') + '|',
		        ECF_FAB			=	ISNULL(RTRIM(LTRIM(LCF.ID_EQUIPAMENTO)), '') + '|',
		        CRO				=	ISNULL(RTRIM(LTRIM(STR(LCF.CONTADOR_REINICIO_OPERACAO))), '0') + '|',
		        CRZ				=	ISNULL(RTRIM(LTRIM(STR(LCF.QTDE_REDUCOES_Z))), '0') + '|',
		        NUM_DOC_INI		=	ISNULL(RTRIM(LTRIM(LCF.CF_INICIAL)), '0') + '|',
		        NUM_DOC_FIN		=	ISNULL(RTRIM(LTRIM(LCF.CF_FINAL)), '0') + '|',
		        DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), LCF.DATA_FISCAL, 103), '/', ''))), '0') + '|',
		        QTD_CANC		=	RTRIM(LTRIM(STR((SELECT QUANTIDADE_CANCELADA = COUNT(*) FROM LOJA_VENDA_PGTO LVP
															WHERE LVP.CODIGO_FILIAL = LCF.CODIGO_FILIAL AND LVP.TERMINAL = LCF.TERMINAL
															AND LVP.DATA = LCF.DATA_FISCAL AND LVP.CANCELADO_FISCAL = 1)))) + '|',
		        VL_DOC			=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_BRUTO, 15, 2))), '0') + '|',
		        VL_CANC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_CANCELADO, 15, 2))), '0') + '|',
		        VL_CANC			=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_CANCELADO, 15, 2))), '0') + '|',
		        VL_DESC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_DESCONTO, 15, 2))), '0') + '|',
		        VL_DESC			=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_DESCONTO, 15, 2))), '0') + '|',
		        VL_ACMO_ICMS	=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_ACRESCIMO, 15, 2))), '0') + '|',
		        VL_ACMO			=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_ACRESCIMO, 15, 2))), '0') + '|',
		        VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT.TOTAL_TARIFA), 15, 2))), '0') + '|',
		        VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT.VALOR_IMPOSTO), 15, 2))), '0') + '|',
		        VL_ISEN			=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_ISENTO, 15, 2))), '0') + '|',
		        VL_NT			=	ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_NAO_TRIBUTAVEL, 15, 2))), '0') + '|',
		        VL_ST			=	'0|', --ISNULL(RTRIM(LTRIM(STR(LCF.TOTAL_SUBSTITUICAO, 15, 2))), '0') + '|',
		  	    LCF.CODIGO_FILIAL, LCF.TERMINAL, LCF.DATA_FISCAL
			FROM LOJA_CONTROLE_FISCAL AS LCF
				LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS AS LCFT ON
					LCF.CODIGO_FILIAL	=	LCFT.CODIGO_FILIAL	AND
					LCF.TERMINAL		=	LCFT.TERMINAL		AND
					LCF.ECF				=	LCFT.ECF			AND
					LCF.ID_EQUIPAMENTO	=	LCFT.ID_EQUIPAMENTO	AND
					LCF.DATA_FISCAL		=	LCFT.DATA_FISCAL	AND
					LCF.CF_INICIAL		=	LCFT.CF_INICIAL		AND
					LCF.SERIE_NF		=	LCFT.SERIE_NF
		    WHERE
				LCFT.ID_IMPOSTO			=	1
				AND LCF.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND LCF.DATA_FISCAL		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY LCF.TERMINAL, LCF.ID_EQUIPAMENTO, LCF.CONTADOR_REINICIO_OPERACAO, LCF.QTDE_REDUCOES_Z, LCF.CF_INICIAL, LCF.CF_FINAL, LCF.DATA_FISCAL, LCF.TOTAL_BRUTO, LCF.TOTAL_CANCELADO, LCF.TOTAL_DESCONTO, LCF.TOTAL_ACRESCIMO, LCF.TOTAL_ISENTO, LCF.TOTAL_NAO_TRIBUTAVEL, LCF.TOTAL_SUBSTITUICAO, LCF.CODIGO_FILIAL
		    ORDER BY LCF.TERMINAL, LCF.ID_EQUIPAMENTO, LCF.CONTADOR_REINICIO_OPERACAO, LCF.QTDE_REDUCOES_Z, LCF.CF_INICIAL, LCF.CF_FINAL, LCF.DATA_FISCAL

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C625
		         SELECT
		            REG				=	'|C625|',
		            COD_ITEM		=	ISNULL(RTRIM(LTRIM(A.PRODUTO)), '') + '|',
		            QTD				=	ISNULL(RTRIM(LTRIM(STR(SUM(A.QTDE), 15, 3))), '') + '|',
		            QTD_CANC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.QTDE_CANCELADA), 15, 3))), '') + '|',
		            UNID			=	ISNULL(RTRIM(LTRIM(P.UNIDADE)), '') + '|',
		            VL_ITEM			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 15, 2))), '') + '|',
		            VL_DESC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.DESCONTO_ITEM > 0 THEN A.DESCONTO_ITEM ELSE 0.00 END), 15, 2))), '') + '|',
		            VL_CANC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN C.VALOR_CANCELADO < 0 THEN 0 ELSE C.VALOR_CANCELADO END), 15, 2))), '') + '|',
		            VL_ACMO_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.DESCONTO_ITEM < 0 THEN ABS(A.DESCONTO_ITEM) ELSE 0.00 END), 15, 2))), '') + '|',
		            VL_BC_ICMS_I	=	ISNULL(RTRIM(LTRIM(STR(SUM((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA))), 15, 2))), '') + '|',
		            ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(A.ALIQUOTA, 6, 2))), '') + '|',
		            VL_ICMS_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(((ISNULL((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 0) * ISNULL(A.ALIQUOTA, 0)) / 100)), 15, 2))), '') + '|',
		            VL_ISEN_I		=	'|',
		            VL_NT_I			=	'|',
		            VL_ST_I			=	'|',
				    D.CODIGO_FILIAL, D.TERMINAL, D.DATA_VENDA
		         FROM LOJA_VENDA_PRODUTO A (NOLOCK)
		            JOIN LOJA_VENDA D (NOLOCK)
			            ON A.CODIGO_FILIAL = D.CODIGO_FILIAL
			            AND A.TICKET = D.TICKET
			            AND A.DATA_VENDA = D.DATA_VENDA
		            JOIN LOJA_VENDA_PGTO C (NOLOCK)
			            ON D.CODIGO_FILIAL_PGTO = C.CODIGO_FILIAL
			            AND D.TERMINAL_PGTO = C.TERMINAL
			            AND D.LANCAMENTO_CAIXA = C.LANCAMENTO_CAIXA
		            JOIN LOJAS_VAREJO G (NOLOCK)
			            ON A.CODIGO_FILIAL	= G.CODIGO_FILIAL
		            JOIN FILIAIS M (NOLOCK)
		        	    ON G.FILIAL	 	= M.FILIAL
		    	    JOIN CADASTRO_CLI_FOR N (NOLOCK)
			            ON M.MATRIZ_FISCAL		= N.NOME_CLIFOR
		            JOIN PRODUTOS P (NOLOCK)
			            ON A.PRODUTO = P.PRODUTO
					JOIN LOJA_CONTROLE_FISCAL B (NOLOCK)
						ON B.CODIGO_FILIAL		= D.CODIGO_FILIAL
						AND B.TERMINAL			= D.TERMINAL
						AND B.DATA_FISCAL		= D.DATA_VENDA
						AND B.ID_EQUIPAMENTO	= C.ID_EQUIPAMENTO
		         WHERE
		            A.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
		            AND C.DATA		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		            AND D.DATA_HORA_CANCELAMENTO IS NULL
		            AND C.NUMERO_CUPOM_FISCAL IS NOT NULL
		            AND A.ALIQUOTA > 0.00
				GROUP BY A.PRODUTO, P.UNIDADE, A.ALIQUOTA, D.CODIGO_FILIAL, D.TERMINAL, D.DATA_VENDA
				ORDER BY A.PRODUTO, P.UNIDADE, A.ALIQUOTA, D.CODIGO_FILIAL, D.TERMINAL, D.DATA_VENDA

----------------------------------------------------------------------------------------------------------------------------------------------------
-- C640
		         SELECT
		            REG				=	'|C640|',
		            COD_ITEM		=	ISNULL(RTRIM(LTRIM(A.PRODUTO)), '') + '|',
		            QTD				=	ISNULL(RTRIM(LTRIM(STR(SUM(A.QTDE), 15, 3))), '') + '|',
		            QTD_CANC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.QTDE_CANCELADA), 15, 3))), '') + '|',
		            UNID			=	ISNULL(RTRIM(LTRIM(P.UNIDADE)), '') + '|',
		            VL_ITEM			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 15, 2))), '') + '|',
		            VL_DESC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.DESCONTO_ITEM > 0 THEN A.DESCONTO_ITEM ELSE 0.00 END), 15, 2))), '') + '|',
		            VL_CANC_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN C.VALOR_CANCELADO < 0 THEN 0 ELSE C.VALOR_CANCELADO END), 15, 2))), '') + '|',
		            VL_ACMO_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.DESCONTO_ITEM < 0 THEN ABS(A.DESCONTO_ITEM) ELSE 0.00 END), 15, 2))), '') + '|',
		            VL_BC_ICMS_I	=	ISNULL(RTRIM(LTRIM(STR(SUM((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA))), 15, 2))), '') + '|',
		            ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(A.ALIQUOTA, 6, 2))), '') + '|',
		            VL_ICMS_I		=	ISNULL(RTRIM(LTRIM(STR(SUM(((ISNULL((A.QTDE * A.PRECO_LIQUIDO * (1-A.FATOR_DESCONTO_VENDA)), 0) * ISNULL(A.ALIQUOTA, 0)) / 100)), 15, 2))), '') + '|',
		            VL_ISEN_I		=	'|',
		            VL_NT_I			=	'|',
		            VL_ST_I			=	'|'
		         FROM LOJA_VENDA_PRODUTO A (NOLOCK)
		            JOIN LOJA_VENDA D (NOLOCK)
			            ON A.CODIGO_FILIAL = D.CODIGO_FILIAL
			            AND A.TICKET = D.TICKET
			            AND A.DATA_VENDA = D.DATA_VENDA
		            JOIN LOJA_VENDA_PGTO C (NOLOCK)
			            ON D.CODIGO_FILIAL_PGTO = C.CODIGO_FILIAL
			            AND D.TERMINAL_PGTO = C.TERMINAL
			            AND D.LANCAMENTO_CAIXA = C.LANCAMENTO_CAIXA
		            JOIN LOJAS_VAREJO G (NOLOCK)
			            ON A.CODIGO_FILIAL	= G.CODIGO_FILIAL
		            JOIN FILIAIS M (NOLOCK)
		        	    ON G.FILIAL	 	= M.FILIAL
		    	    JOIN CADASTRO_CLI_FOR N (NOLOCK)
			            ON M.MATRIZ_FISCAL		= N.NOME_CLIFOR
		            JOIN PRODUTOS P (NOLOCK)
			            ON A.PRODUTO = P.PRODUTO
					JOIN LOJA_CONTROLE_FISCAL B (NOLOCK)
						ON B.CODIGO_FILIAL		= D.CODIGO_FILIAL
						AND B.TERMINAL			= D.TERMINAL
						AND B.DATA_FISCAL		= D.DATA_VENDA
						AND B.ID_EQUIPAMENTO	= C.ID_EQUIPAMENTO
		         WHERE
		            A.CODIGO_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
		            AND C.DATA		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		            AND D.DATA_HORA_CANCELAMENTO IS NULL
		            AND C.NUMERO_CUPOM_FISCAL IS NOT NULL
		            AND A.ALIQUOTA > 0.00
				GROUP BY A.PRODUTO, P.UNIDADE, A.ALIQUOTA
				ORDER BY A.PRODUTO, P.UNIDADE, A.ALIQUOTA

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E020
			SELECT
				REG				=	'|E020|',
				IND_OPER		=	'0|',
				IND_EMIT		=	CASE WHEN A.NF_PROPRIA_EMITIDA = 0 THEN '1' ELSE '0' END + '|',
				COD_PART		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				COD_MOD			=	'01|',
				COD_SIT			=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER				=	ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF_ENTRADA))) + '|',
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
				DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
				DT_E_S			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.RECEBIMENTO, 103), '/', ''))), '') + '|',
				VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ST			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 12 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_COMPL		=	ISNULL(RTRIM(LTRIM(STR(0, 15, 2))), '') + '|',
				IND_COMPL		=	'00|',
				VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO_ISENTO ELSE 0 END), 15, 2))), '') + '|',
				VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO_OUTROS ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_IPI			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ISNT_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO_ISENTO ELSE 0 END), 15, 2))), '') + '|',
				VL_OUT_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO_OUTROS ELSE 0 END), 15, 2))), '') + '|',
				COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				SERIE			=	A.SERIE_NF_ENTRADA,
				COD_CLIFOR		=	ISNULL(A.CODIGO_CLIENTE, A.COD_CLIFOR), 
				A.COD_FILIAL
			FROM
				W_LF_REGISTRO_ENTRADA_IMPOSTO AS A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF_ENTRADA = S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE = S.ESPECIE_SERIE
				LEFT JOIN CADASTRO_CLI_FOR AS C ON
					A.COD_CLIFOR				=	C.COD_CLIFOR
				LEFT JOIN CLIENTES_VAREJO AS CV ON
					A.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
			WHERE  A.CODIGO_FISCAL_OPERACAO NOT IN ('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
													'1256', '2256', '1257', '2257',
													'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
													'1306', '2306',
													'1350', '1352', '1353', '1354', '1355', '1356', '1932',
													'2350', '2352', '2353', '2354', '2355', '2356', '2932',
													'3350', '3352', '3353', '3354', '3355', '3356',
													'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
													'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
													'1932', '2932')
				AND	A.ID_IMPOSTO IN (1, 2, 12)
				AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.RECEBIMENTO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				CASE WHEN A.NF_PROPRIA_EMITIDA = 0 THEN '1' ELSE '0' END + '|',
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF_ENTRADA))) + '|',
				ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.RECEBIMENTO, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				A.SERIE_NF_ENTRADA,
				ISNULL(A.CODIGO_CLIENTE, A.COD_CLIFOR),
				A.COD_FILIAL

			UNION ALL

			SELECT 	DISTINCT
				REG				=	'|E020|',
				IND_OPER		=	'1|',
				IND_EMIT		=	'0|',
				COD_PART		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				COD_MOD			=	'01|',
				COD_SIT			=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER				=	ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF))) + '|',
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
				DT_E_S			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_SAIDA, 103), '/', ''))), '') + '|',
				VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ST			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 12 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_COMPL		=	ISNULL(RTRIM(LTRIM(STR(0, 15, 2))), '') + '|',
				IND_COMPL		=	'00|',
				VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO_ISENTO ELSE 0 END), 15, 2))), '') + '|',
				VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO_OUTROS ELSE 0 END), 15, 2))), '') + '|',
				VL_BC_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_IPI			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
				VL_ISNT_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO_ISENTO ELSE 0 END), 15, 2))), '') + '|',
				VL_OUT_IPI		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 2 THEN A.VALOR_IMPOSTO_OUTROS ELSE 0 END), 15, 2))), '') + '|',
				COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				SERIE			=	A.SERIE_NF,
				COD_CLIFOR		=	ISNULL(A.CODIGO_CLIENTE, A.COD_CLIFOR), 
				A.COD_FILIAL
			FROM
				W_LF_REGISTRO_SAIDA_IMPOSTO AS A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF = S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE = S.ESPECIE_SERIE
				LEFT JOIN CADASTRO_CLI_FOR AS C ON
					A.COD_CLIFOR				=	C.COD_CLIFOR
				LEFT JOIN CLIENTES_VAREJO AS CV ON
					A.CODIGO_CLIENTE = CV.CODIGO_CLIENTE
			WHERE  A.CODIGO_FISCAL_OPERACAO NOT IN ('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
													'5256', '6256', '5257', '6257', '5258', '6258',
													'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
													'5306', '6306', '5307', '6307',
													'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
													'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
													'7350', '7358',
													'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
													'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
													'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
													'5932', '6932')
				AND ID_IMPOSTO IN (1, 2, 12)
				AND A.ESPECIE NOT IN ('ECF', 'CF')
				AND A.SERIE_NF <> 'D1'
				AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.CODIGO_CLIENTE)), RTRIM(LTRIM(A.COD_CLIFOR))) END + '|',
				CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				ISNULL(RTRIM(LTRIM(A.SERIE_NF_OFICIAL)), RTRIM(LTRIM(A.SERIE_NF))) + '|',
				ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
				ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_SAIDA, 103), '/', ''))), '') + '|',
				ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				A.SERIE_NF,
				ISNULL(A.CODIGO_CLIENTE, A.COD_CLIFOR), 
				A.COD_FILIAL

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E025
			SELECT
				REG				=	'|E025|',
				CFOP			=	ISNULL(RTRIM(LTRIM(LFEI_ICMS.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_CONT_P		=	ISNULL(SUM(LFEI_ICMS.VALOR_CONTABIL), 0.00),
				VL_BC_ICMS_P	=	ISNULL(SUM(LFEI_ICMS.BASE_IMPOSTO), 0.00),
				ALIQ_ICMS		=	ISNULL(LFEI_ICMS.TAXA_IMPOSTO, 0),
				VL_ICMS_P		=	ISNULL(SUM(LFEI_ICMS.VALOR_IMPOSTO), 0.00),
				VL_ST_P			=	ISNULL(SUM(LFEI_ICMS_ST.VALOR_IMPOSTO_ST), 0.00),
				VL_COMPL_P		=	CAST(0.00 AS NUMERIC(19,2)),
				VL_ISNT_ICMS_P	=	ISNULL(SUM(LFEI_ICMS.VALOR_IMPOSTO_ISENTO), 0.00),
				VL_OUT_ICMS_P	=	ISNULL(SUM(LFEI_ICMS.VALOR_IMPOSTO_OUTROS), 0.00),
				VL_BC_IPI_P		=	ISNULL(SUM(LFEI_IPI.BASE_IMPOSTO_IPI), 0.00),
				VL_IPI_P		=	ISNULL(SUM(LFEI_IPI.VALOR_IMPOSTO_IPI), 0.00),
				VL_ISNT_IPI_P	=	ISNULL(SUM(LFEI_IPI.VALOR_IMPOSTO_ISENTO_IPI), 0.00),
				VL_OUT_IPI_P	=	ISNULL(SUM(LFEI_IPI.VALOR_IMPOSTO_OUTROS_IPI), 0.00),
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(LFEI_ICMS.NF_ENTRADA)), '') + '|',
				SERIE			=	LFEI_ICMS.SERIE_NF_ENTRADA,
				COD_CLIFOR		=	ISNULL(LFEI_ICMS.CODIGO_CLIENTE, LFEI_ICMS.COD_CLIFOR), 
				LFEI_ICMS.COD_FILIAL
			FROM W_LF_REGISTRO_ENTRADA_IMPOSTO_ITEM AS LFEI_ICMS
				LEFT JOIN
					(SELECT LFEI_ICMS_ST.ITEM_IMPRESSAO, LFEI_ICMS_ST.COD_MATRIZ_FISCAL, LFEI_ICMS_ST.COD_CLIFOR, LFEI_ICMS_ST.NF_ENTRADA,
							LFEI_ICMS_ST.SERIE_NF_ENTRADA, LFEI_ICMS_ST.ID_IMPOSTO, LFEI_ICMS_ST.CODIGO_FISCAL_OPERACAO, LFEI_ICMS_ST.TAXA_IMPOSTO, LFEI_ICMS_ST.ITEM,
						VALOR_IMPOSTO_ST			=	ISNULL(SUM(LFEI_ICMS_ST.VALOR_IMPOSTO), 0.00)
					FROM
						W_LF_REGISTRO_ENTRADA_IMPOSTO_ITEM AS LFEI_ICMS_ST
					WHERE
						LFEI_ICMS_ST.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
															'1256', '2256', '1257', '2257',
															'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
															'1306', '2306',
															'1350', '1352', '1353', '1354', '1355', '1356', '1932',
															'2350', '2352', '2353', '2354', '2355', '2356', '2932',
															'3350', '3352', '3353', '3354', '3355', '3356',
															'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
															'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
															'1932', '2932')										AND
						LFEI_ICMS_ST.ID_IMPOSTO				=		12											AND
						LFEI_ICMS_ST.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'				AND
						LFEI_ICMS_ST.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					GROUP BY
						LFEI_ICMS_ST.ITEM_IMPRESSAO, LFEI_ICMS_ST.COD_MATRIZ_FISCAL, LFEI_ICMS_ST.COD_CLIFOR, LFEI_ICMS_ST.NF_ENTRADA,
							LFEI_ICMS_ST.SERIE_NF_ENTRADA, LFEI_ICMS_ST.ID_IMPOSTO, LFEI_ICMS_ST.CODIGO_FISCAL_OPERACAO, LFEI_ICMS_ST.TAXA_IMPOSTO, LFEI_ICMS_ST.ITEM) AS LFEI_ICMS_ST ON
						LFEI_ICMS.ITEM_IMPRESSAO			=	LFEI_ICMS_ST.ITEM_IMPRESSAO			AND
						LFEI_ICMS.COD_MATRIZ_FISCAL			=	LFEI_ICMS_ST.COD_MATRIZ_FISCAL		AND
						LFEI_ICMS.COD_CLIFOR				=	LFEI_ICMS_ST.COD_CLIFOR				AND
						LFEI_ICMS.NF_ENTRADA				=	LFEI_ICMS_ST.NF_ENTRADA				AND
						LFEI_ICMS.SERIE_NF_ENTRADA			=	LFEI_ICMS_ST.SERIE_NF_ENTRADA		AND
						--LFEI_ICMS.ID_IMPOSTO				=	LFEI_ICMS_ST.ID_IMPOSTO				AND
						LFEI_ICMS.CODIGO_FISCAL_OPERACAO	=	LFEI_ICMS_ST.CODIGO_FISCAL_OPERACAO	--AND
						--LFEI_ICMS.TAXA_IMPOSTO				=	LFEI_ICMS_ST.TAXA_IMPOSTO			AND
						--LFEI_ICMS.ITEM						=	LFEI_ICMS_ST.ITEM
				LEFT JOIN
					(SELECT LFEI_IPI.ITEM_IMPRESSAO, LFEI_IPI.COD_MATRIZ_FISCAL, LFEI_IPI.COD_CLIFOR, LFEI_IPI.NF_ENTRADA,
							LFEI_IPI.SERIE_NF_ENTRADA, LFEI_IPI.ID_IMPOSTO, LFEI_IPI.CODIGO_FISCAL_OPERACAO, LFEI_IPI.TAXA_IMPOSTO, LFEI_IPI.ITEM,
							BASE_IMPOSTO_IPI			=	ISNULL(SUM(LFEI_IPI.BASE_IMPOSTO), 0.00),
							VALOR_IMPOSTO_IPI			=	ISNULL(SUM(LFEI_IPI.VALOR_IMPOSTO), 0.00),
							VALOR_IMPOSTO_ISENTO_IPI	=	ISNULL(SUM(LFEI_IPI.VALOR_IMPOSTO_ISENTO), 0.00),
							VALOR_IMPOSTO_OUTROS_IPI	=	ISNULL(SUM(LFEI_IPI.VALOR_IMPOSTO_OUTROS), 0.00)
					FROM
						W_LF_REGISTRO_ENTRADA_IMPOSTO_ITEM AS LFEI_IPI
					WHERE
						LFEI_IPI.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
															'1256', '2256', '1257', '2257',
															'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
															'1306', '2306',
															'1350', '1352', '1353', '1354', '1355', '1356', '1932',
															'2350', '2352', '2353', '2354', '2355', '2356', '2932',
															'3350', '3352', '3353', '3354', '3355', '3356',
															'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
															'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
															'1932', '2932')									AND
						LFEI_IPI.ID_IMPOSTO				=		2											AND
						LFEI_IPI.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'				AND
						LFEI_IPI.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					GROUP BY
						LFEI_IPI.ITEM_IMPRESSAO, LFEI_IPI.COD_MATRIZ_FISCAL, LFEI_IPI.COD_CLIFOR, LFEI_IPI.NF_ENTRADA,
							LFEI_IPI.SERIE_NF_ENTRADA, LFEI_IPI.ID_IMPOSTO, LFEI_IPI.CODIGO_FISCAL_OPERACAO, LFEI_IPI.TAXA_IMPOSTO, LFEI_IPI.ITEM) AS LFEI_IPI ON
						LFEI_ICMS.ITEM_IMPRESSAO			=	LFEI_IPI.ITEM_IMPRESSAO			AND
						LFEI_ICMS.COD_MATRIZ_FISCAL			=	LFEI_IPI.COD_MATRIZ_FISCAL		AND
						LFEI_ICMS.COD_CLIFOR				=	LFEI_IPI.COD_CLIFOR				AND
						LFEI_ICMS.NF_ENTRADA				=	LFEI_IPI.NF_ENTRADA				AND
						LFEI_ICMS.SERIE_NF_ENTRADA			=	LFEI_IPI.SERIE_NF_ENTRADA		AND
						--LFEI_ICMS.ID_IMPOSTO				=	LFEI_IPI.ID_IMPOSTO				AND
						LFEI_ICMS.CODIGO_FISCAL_OPERACAO	=	LFEI_IPI.CODIGO_FISCAL_OPERACAO	--AND
						--LFEI_ICMS.TAXA_IMPOSTO				=	LFEI_IPI.TAXA_IMPOSTO			AND
						--LFEI_ICMS.ITEM						=	LFEI_IPI.ITEM
			WHERE
				LFEI_ICMS.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
													'1256', '2256', '1257', '2257',
													'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
													'1306', '2306',
													'1350', '1352', '1353', '1354', '1355', '1356', '1932',
													'2350', '2352', '2353', '2354', '2355', '2356', '2932',
													'3350', '3352', '3353', '3354', '3355', '3356',
													'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
													'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
													'1932', '2932')									AND
				LFEI_ICMS.ID_IMPOSTO				=		1										AND
				LFEI_ICMS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
				LFEI_ICMS.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY 
				ISNULL(RTRIM(LTRIM(LFEI_ICMS.CODIGO_FISCAL_OPERACAO)), '') + '|',
				ISNULL(LFEI_ICMS.TAXA_IMPOSTO, 0),
				ISNULL(RTRIM(LTRIM(LFEI_ICMS.NF_ENTRADA)), '') + '|',
				LFEI_ICMS.SERIE_NF_ENTRADA,
				ISNULL(LFEI_ICMS.CODIGO_CLIENTE, LFEI_ICMS.COD_CLIFOR), 
				LFEI_ICMS.COD_FILIAL

UNION ALL

			SELECT
				REG				=	'|E025|',
				CFOP			=	ISNULL(RTRIM(LTRIM(LFSI_ICMS.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_CONT_P		=	ISNULL(SUM(LFSI_ICMS.VALOR_CONTABIL), 0.00),
				VL_BC_ICMS_P	=	ISNULL(SUM(LFSI_ICMS.BASE_IMPOSTO), 0.00),
				ALIQ_ICMS		=	ISNULL(LFSI_ICMS.TAXA_IMPOSTO, 0),
				VL_ICMS_P		=	ISNULL(SUM(LFSI_ICMS.VALOR_IMPOSTO), 0.00),
				VL_ST_P			=	ISNULL(SUM(LFSI_ICMS_ST.VALOR_IMPOSTO_ST), 0.00),
				VL_COMPL_P		=	CAST(0.00 AS NUMERIC(19,2)),
				VL_ISNT_ICMS_P	=	ISNULL(SUM(LFSI_ICMS.VALOR_IMPOSTO_ISENTO), 0.00),
				VL_OUT_ICMS_P	=	ISNULL(SUM(LFSI_ICMS.VALOR_IMPOSTO_OUTROS), 0.00),
				VL_BC_IPI_P		=	ISNULL(SUM(LFSI_IPI.BASE_IMPOSTO_IPI), 0.00),
				VL_IPI_P		=	ISNULL(SUM(LFSI_IPI.VALOR_IMPOSTO_IPI), 0.00),
				VL_ISNT_IPI_P	=	ISNULL(SUM(LFSI_IPI.VALOR_IMPOSTO_ISENTO_IPI), 0.00),
				VL_OUT_IPI_P	=	ISNULL(SUM(LFSI_IPI.VALOR_IMPOSTO_OUTROS_IPI), 0.00),
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(LFSI_ICMS.NF_SAIDA)), '') + '|',
				SERIE			=	LFSI_ICMS.SERIE_NF,
				COD_CLIFOR		=	ISNULL(LFSI_ICMS.CODIGO_CLIENTE, LFSI_ICMS.COD_CLIFOR), 
				LFSI_ICMS.COD_FILIAL
			FROM W_LF_REGISTRO_SAIDA_IMPOSTO_ITEM AS LFSI_ICMS
				LEFT JOIN
					(SELECT LFSI_ICMS_ST.ITEM_IMPRESSAO, LFSI_ICMS_ST.COD_FILIAL, LFSI_ICMS_ST.NF_SAIDA, LFSI_ICMS_ST.SERIE_NF, LFSI_ICMS_ST.NF_SAIDA_FINAL,
							LFSI_ICMS_ST.COD_MATRIZ_FISCAL, LFSI_ICMS_ST.CODIGO_FISCAL_OPERACAO, LFSI_ICMS_ST.ID_IMPOSTO, LFSI_ICMS_ST.TAXA_IMPOSTO, LFSI_ICMS_ST.ITEM,
						VALOR_IMPOSTO_ST			=	ISNULL(SUM(LFSI_ICMS_ST.VALOR_IMPOSTO), 0.00)
					FROM
						W_LF_REGISTRO_SAIDA_IMPOSTO_ITEM AS LFSI_ICMS_ST
					WHERE
						LFSI_ICMS_ST.CODIGO_FISCAL_OPERACAO	NOT IN	('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
															'5256', '6256', '5257', '6257', '5258', '6258',
															'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
															'5306', '6306', '5307', '6307',
															'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
															'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
															'7350', '7358',
															'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
															'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
															'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
															'5932', '6932')									AND
						LFSI_ICMS_ST.ID_IMPOSTO				=		12										AND
						LFSI_ICMS_ST.ESPECIE				NOT IN	('ECF', 'CF')							AND
						LFSI_ICMS_ST.SERIE_NF				<>		'D1'									AND
						LFSI_ICMS_ST.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
						LFSI_ICMS_ST.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					GROUP BY
						LFSI_ICMS_ST.ITEM_IMPRESSAO, LFSI_ICMS_ST.COD_FILIAL, LFSI_ICMS_ST.NF_SAIDA, LFSI_ICMS_ST.SERIE_NF, LFSI_ICMS_ST.NF_SAIDA_FINAL,
						LFSI_ICMS_ST.COD_MATRIZ_FISCAL, LFSI_ICMS_ST.CODIGO_FISCAL_OPERACAO, LFSI_ICMS_ST.ID_IMPOSTO, LFSI_ICMS_ST.TAXA_IMPOSTO, 
						LFSI_ICMS_ST.ITEM, LFSI_ICMS_ST.CODIGO_FISCAL_OPERACAO) AS LFSI_ICMS_ST ON
					LFSI_ICMS.ITEM_IMPRESSAO			=	LFSI_ICMS_ST.ITEM_IMPRESSAO			AND
					LFSI_ICMS.COD_FILIAL				=	LFSI_ICMS_ST.COD_FILIAL				AND
					LFSI_ICMS.NF_SAIDA					=	LFSI_ICMS_ST.NF_SAIDA				AND
					LFSI_ICMS.SERIE_NF					=	LFSI_ICMS_ST.SERIE_NF				AND
					LFSI_ICMS.NF_SAIDA_FINAL			=	LFSI_ICMS_ST.NF_SAIDA_FINAL			AND
					LFSI_ICMS.COD_MATRIZ_FISCAL			=	LFSI_ICMS_ST.COD_MATRIZ_FISCAL		AND
					LFSI_ICMS.CODIGO_FISCAL_OPERACAO	=	LFSI_ICMS_ST.CODIGO_FISCAL_OPERACAO	--AND
					--LFSI_ICMS.ID_IMPOSTO				=	LFSI_ICMS_ST.ID_IMPOSTO				AND
					--LFSI_ICMS.TAXA_IMPOSTO				=	LFSI_ICMS_ST.TAXA_IMPOSTO			AND
					--LFSI_ICMS.ITEM						=	LFSI_ICMS_ST.ITEM
				LEFT JOIN
					(SELECT LFSI_IPI.ITEM_IMPRESSAO, LFSI_IPI.COD_FILIAL, LFSI_IPI.NF_SAIDA, LFSI_IPI.SERIE_NF, LFSI_IPI.NF_SAIDA_FINAL,
							LFSI_IPI.COD_MATRIZ_FISCAL, LFSI_IPI.CODIGO_FISCAL_OPERACAO, LFSI_IPI.ID_IMPOSTO, LFSI_IPI.TAXA_IMPOSTO, LFSI_IPI.ITEM,
							BASE_IMPOSTO_IPI			=	ISNULL(SUM(LFSI_IPI.BASE_IMPOSTO), 0.00),
							VALOR_IMPOSTO_IPI			=	ISNULL(SUM(LFSI_IPI.VALOR_IMPOSTO), 0.00),
							VALOR_IMPOSTO_ISENTO_IPI	=	ISNULL(SUM(LFSI_IPI.VALOR_IMPOSTO_ISENTO), 0.00),
							VALOR_IMPOSTO_OUTROS_IPI	=	ISNULL(SUM(LFSI_IPI.VALOR_IMPOSTO_OUTROS), 0.00)
					FROM
						W_LF_REGISTRO_SAIDA_IMPOSTO_ITEM AS LFSI_IPI
					WHERE
						LFSI_IPI.CODIGO_FISCAL_OPERACAO	NOT IN	('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
															'5256', '6256', '5257', '6257', '5258', '6258',
															'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
															'5306', '6306', '5307', '6307',
															'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
															'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
															'7350', '7358',
															'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
															'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
															'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
															'5932', '6932')								AND
						LFSI_IPI.ID_IMPOSTO				=		2										AND
						LFSI_IPI.ESPECIE				NOT IN	('ECF', 'CF')							AND
						LFSI_IPI.SERIE_NF				<>		'D1'									AND
						LFSI_IPI.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
						LFSI_IPI.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					GROUP BY
						LFSI_IPI.ITEM_IMPRESSAO, LFSI_IPI.COD_FILIAL, LFSI_IPI.NF_SAIDA, LFSI_IPI.SERIE_NF, LFSI_IPI.NF_SAIDA_FINAL,
						LFSI_IPI.COD_MATRIZ_FISCAL, LFSI_IPI.CODIGO_FISCAL_OPERACAO, LFSI_IPI.ID_IMPOSTO, LFSI_IPI.TAXA_IMPOSTO, LFSI_IPI.ITEM,
						LFSI_IPI.CODIGO_FISCAL_OPERACAO) AS LFSI_IPI ON
					LFSI_ICMS.ITEM_IMPRESSAO			=	LFSI_IPI.ITEM_IMPRESSAO			AND
					LFSI_ICMS.COD_FILIAL				=	LFSI_IPI.COD_FILIAL				AND
					LFSI_ICMS.NF_SAIDA					=	LFSI_IPI.NF_SAIDA				AND
					LFSI_ICMS.SERIE_NF					=	LFSI_IPI.SERIE_NF				AND
					LFSI_ICMS.NF_SAIDA_FINAL			=	LFSI_IPI.NF_SAIDA_FINAL			AND
					LFSI_ICMS.COD_MATRIZ_FISCAL			=	LFSI_IPI.COD_MATRIZ_FISCAL		AND
					LFSI_ICMS.CODIGO_FISCAL_OPERACAO	=	LFSI_IPI.CODIGO_FISCAL_OPERACAO	--AND
					--LFSI_ICMS.ID_IMPOSTO				=	LFSI_IPI.ID_IMPOSTO				AND
					--LFSI_ICMS.TAXA_IMPOSTO				=	LFSI_IPI.TAXA_IMPOSTO			AND
					--LFSI_ICMS.ITEM						=	LFSI_IPI.ITEM
			WHERE
				LFSI_ICMS.CODIGO_FISCAL_OPERACAO	NOT IN	('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
													'5256', '6256', '5257', '6257', '5258', '6258',
													'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
													'5306', '6306', '5307', '6307',
													'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
													'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
													'7350', '7358',
													'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
													'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
													'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
													'5932', '6932')								AND
				LFSI_ICMS.ID_IMPOSTO			=		1										AND
				LFSI_ICMS.ESPECIE				NOT IN	('ECF', 'CF')							AND
				LFSI_ICMS.SERIE_NF				<>		'D1'									AND
				LFSI_ICMS.COD_FILIAL			=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
				LFSI_ICMS.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY 
				ISNULL(RTRIM(LTRIM(LFSI_ICMS.CODIGO_FISCAL_OPERACAO)), '') + '|',
				ISNULL(LFSI_ICMS.TAXA_IMPOSTO, 0),
				ISNULL(RTRIM(LTRIM(LFSI_ICMS.NF_SAIDA)), '') + '|',
				LFSI_ICMS.SERIE_NF,
				ISNULL(LFSI_ICMS.CODIGO_CLIENTE, LFSI_ICMS.COD_CLIFOR), 
				LFSI_ICMS.COD_FILIAL
			ORDER BY NUM_DOC, SERIE, COD_CLIFOR, COD_FILIAL, CFOP, ALIQ_ICMS

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E050
		SELECT 	DISTINCT
			REG				=	'|E050|',
			COD_MOD			=	'02|',
			SER				=	ISNULL(RTRIM(LTRIM(S.COD_SERIE_SINTEGRA)), '') + '|',
			SUB				=	'|',
			NUM_DOC_INI		=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
			NUM_DOC_FIN		=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
			DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
			NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
			QTD_CANC		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '1' ELSE '0' END + '|',
			VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_CONTABIL ELSE 0 END), 15, 2))), '') + '|',
			VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.BASE_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
			VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO ELSE 0 END), 15, 2))), '') + '|',
			VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO_ISENTO ELSE 0 END), 15, 2))), '') + '|',
			VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(CASE WHEN A.ID_IMPOSTO = 1 THEN A.VALOR_IMPOSTO_OUTROS ELSE 0 END), 15, 2))), '') + '|',
			COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
			SERIE			=	A.SERIE_NF,
			CLI_FILIAL		=	FILIAL.NOME_CLIFOR,
			A.CODIGO_FISCAL_OPERACAO
			FROM LF_REGISTRO_SAIDA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF			=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE	=	S.ESPECIE_SERIE
				LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
					A.COD_FILIAL		=	FILIAL.COD_CLIFOR
		WHERE A.SERIE_NF = 'D1'
			AND ID_IMPOSTO = 1
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY S.COD_SERIE_SINTEGRA, A.NF_SAIDA, A.EMISSAO, A.CTB_LANCAMENTO, A.NOTA_CANCELADA, A.OBS,
			A.SERIE_NF, FILIAL.NOME_CLIFOR, A.CODIGO_FISCAL_OPERACAO

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E055
		SELECT 	DISTINCT
			REG				=	'|E055|',
			CFOP			=	ISNULL(RTRIM(LTRIM(A.CODIGO_FISCAL_OPERACAO)), '') + '|',
			A.ID_IMPOSTO, A.VALOR_CONTABIL, A.BASE_IMPOSTO, A.VALOR_IMPOSTO,
			A.VALOR_IMPOSTO_ISENTO, A.VALOR_IMPOSTO_OUTROS, A.TAXA_IMPOSTO,
			NUM_DOC			=	A.NF_SAIDA,
			SERIE			=	A.SERIE_NF,
			CLI_FILIAL		=	FILIAL.NOME_CLIFOR
		FROM LF_REGISTRO_SAIDA A
			LEFT JOIN CADASTRO_CLI_FOR AS FILIAL ON
				A.COD_FILIAL = FILIAL.COD_CLIFOR
		WHERE A.SERIE_NF = 'D1'
			AND A.NOTA_CANCELADA = 0
			AND ID_IMPOSTO = 1
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E060
		SELECT
			REG				=	'|E060|',
			COD_MOD			=	'2D|',
			ECF_CX			=	ISNULL(RTRIM(LTRIM(LCF.TERMINAL)), '0') + '|',
			ECF_FAB			=	ISNULL(RTRIM(LTRIM(LCF.ID_EQUIPAMENTO)), '') + '|',
			CRO				=	ISNULL(RTRIM(LTRIM(LCF.CONTADOR_REINICIO_OPERACAO)), '0') + '|',
			CRZ				=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.QTDE_REDUCOES_Z)))), '0') + '|',
			DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), LCF.DATA_FISCAL, 103), '/', ''))), '') + '|',
			NUM_DOC_INI		=	ISNULL(RTRIM(LTRIM(STR(LCF.CF_INICIAL))), '0') + '|',
			NUM_DOC_FIN		=	ISNULL(RTRIM(LTRIM(STR(LCF.CF_FINAL))), '0') + '|',
			GT_INI			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.GT_INICIAL), 15, 2))), '0.00') + '|',
			GT_FIN			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.GT_FINAL), 15, 2))), '0.00') + '|',
			VL_BRT			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_BRUTO), 15, 2))), '0.00') + '|',
			VL_CANC_ISSQN	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_CANC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_CANCELADO), 15, 2))), '0.00') + '|',
			VL_CANC			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_CANCELADO), 15, 2))), '0.00') + '|',
			VL_DESC_ISSQN	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_DESC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_DESCONTO), 15, 2))), '0.00') + '|',
			VL_DESC			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_DESCONTO), 15, 2))), '0.00') + '|',
			VL_ACMO_ISSQN	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ACMO_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ACMO			=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ISSQN		=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ISS.VL_ISSQN), 15, 2))), '0.00') + '|',
			VL_LIQ			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_BRUTO - LCF.TOTAL_DESCONTO - LCF.TOTAL_CANCELADO - LCF.TOTAL_ISENTO - LCF.TOTAL_NAO_TRIBUTAVEL - LCF.TOTAL_SUBSTITUICAO), 15, 2))), '0.00') + '|',
			VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ICMS.VL_BC_ICMS), 15, 2))), '0.00') + '|',
			VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ICMS.VL_ICMS), 15, 2))), '0.00') + '|',
			VL_ISEN			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ICMS.VL_ISEN), 15, 2))), '0.00') + '|',
			VL_NT			=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ST			=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(LCF.OBS)), '') + '|'
		FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
			JOIN LOJAS_VAREJO AS FIL (NOLOCK)
				ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
			JOIN FILIAIS (NOLOCK)
				ON FILIAIS.FILIAL = FIL.FILIAL
			LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
				ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO IN (14, 22)
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
							ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ISS.TERMINAL
							AND LCF.ECF = LCFT_ISS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							ALIQ_ICMS = LCFT.PER_TARIFA,
							VL_BC_ICMS = SUM(LCFT.TOTAL_TARIFA),
							VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END),
							VL_ISEN = SUM(LCFT.VALOR_ISENTO)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO  = 1
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.PER_TARIFA) AS LCFT_ICMS
							ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
							AND LCF.ECF = LCFT_ICMS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
		WHERE (RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
			AND LCF.ECF <> 0
			AND LCF.SERIE_NF <> 'D1'
			AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
			AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY LCF.TERMINAL, LCF.ID_EQUIPAMENTO, LCF.CONTADOR_REINICIO_OPERACAO, LCF.DATA_FISCAL,
				LCF.CF_INICIAL, LCF.CF_FINAL, LCF.OBS

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E065
		SELECT
			REG				=	'|E065|',
			LCF.TOTAL_CANCELADO, LCF.TOTAL_DESCONTO, LCFT_ISS.VL_ISSQN, LCFT_ICMS.VL_BC_ICMS,
			LCFT_ICMS.ALIQ_ICMS, LCFT_ICMS.VL_ICMS, LCFT_ICMS.VL_ISEN,
			ECF_CX			=	ISNULL(RTRIM(LTRIM(LCF.TERMINAL)), '0') + '|',
			ECF_FAB			=	ISNULL(RTRIM(LTRIM(LCF.ID_EQUIPAMENTO)), '') + '|',
			CRO				=	ISNULL(RTRIM(LTRIM(LCF.CONTADOR_REINICIO_OPERACAO)), '0') + '|',
			DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), LCF.DATA_FISCAL, 103), '/', ''))), '') + '|',
			NUM_DOC_INI		=	ISNULL(RTRIM(LTRIM(STR(LCF.CF_INICIAL))), '0') + '|',
			NUM_DOC_FIN		=	ISNULL(RTRIM(LTRIM(STR(LCF.CF_FINAL))), '0') + '|',
			COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(LCF.OBS)), '') + '|'
		FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
			JOIN LOJAS_VAREJO AS FIL (NOLOCK)
				ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
			JOIN FILIAIS (NOLOCK)
				ON FILIAIS.FILIAL = FIL.FILIAL
			LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
				ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO IN (14, 22)
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
							ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ISS.TERMINAL
							AND LCF.ECF = LCFT_ISS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							ALIQ_ICMS = LCFT.PER_TARIFA,
							VL_BC_ICMS = SUM(LCFT.TOTAL_TARIFA),
							VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END),
							VL_ISEN = SUM(LCFT.VALOR_ISENTO)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO  = 1
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.PER_TARIFA) AS LCFT_ICMS
							ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
							AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
							AND LCF.ECF = LCFT_ICMS.ECF
							AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
							AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
							AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
							AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
		WHERE (RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
			AND LCF.ECF <> 0
			AND LCF.SERIE_NF <> 'D1'
			AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
			AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E080
		SELECT
			REG				=	'|E080|',
			IND_MR			=	'0|',
			NUM_MR_ECF		=	ISNULL(RTRIM(LTRIM(STR(LCF.NR_MAPA_RESUMO))), '0') + '|',
			NUM_LCTO		=	'|',
			DT_MR_ECF		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), LCF.DATA_FISCAL, 103), '/', ''))), '') + '|',
			VL_BRT			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_BRUTO), 15, 2))), '0.00') + '|',
			VL_CANC_ISSQN	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_CANC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_CANCELADO), 15, 2))), '0.00') + '|',
			VL_CANC			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_CANCELADO), 15, 2))), '0.00') + '|',
			VL_DESC_ISSQN	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_DESC_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_DESCONTO), 15, 2))), '0.00') + '|',
			VL_DESC			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_DESCONTO), 15, 2))), '0.00') + '|',
			VL_ACMO_ISSQN	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ACMO_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ACMO			=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ISSQN		=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ISS.VL_ISSQN), 15, 2))), '0.00') + '|',
			VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCF.TOTAL_BRUTO - LCF.TOTAL_DESCONTO - LCF.TOTAL_CANCELADO - LCF.TOTAL_ISENTO - LCF.TOTAL_NAO_TRIBUTAVEL - LCF.TOTAL_SUBSTITUICAO), 15, 2))), '0.00') + '|',
			VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ICMS.VL_BC_ICMS), 15, 2))), '0.00') + '|',
			VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ICMS.VL_ICMS), 15, 2))), '0.00') + '|',
			VL_ISEN			=	ISNULL(RTRIM(LTRIM(STR(SUM(LCFT_ICMS.VL_ISEN), 15, 2))), '0.00') + '|',
			VL_NT			=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			VL_ST			=	ISNULL(RTRIM(LTRIM(STR(SUM(0), 15, 2))), '0.00') + '|',
			IND_OBS			=	CASE WHEN RTRIM(LTRIM(ISNULL(LCF.OBS, ''))) = '' THEN '0|' ELSE '1|' END
		FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
			JOIN LOJAS_VAREJO AS FIL (NOLOCK)
				ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
			JOIN FILIAIS (NOLOCK)
				ON FILIAIS.FILIAL = FIL.FILIAL
			LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
				ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
			LEFT JOIN (SELECT A.DATA_FISCAL, A.CF_INICIAL,
							A.CODIGO_FILIAL, A.TERMINAL, A.ID_EQUIPAMENTO, A.ECF, A.SERIE_NF,
							VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO AS FIL (NOLOCK)
								ON A.CODIGO_FILIAL = FIL.CODIGO_FILIAL
							JOIN FILIAIS (NOLOCK)
								ON FILIAIS.FILIAL = FIL.FILIAL
							LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
								ON A.ESPECIE_SERIE = ESP.ESPECIE_SERIE
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO IN (14, 22)
								AND (RTRIM(ESP.DESC_ESPECIE_SERIE) = 'CF' OR  RTRIM(ESP.DESC_ESPECIE_SERIE) = 'ECF' OR ESP.DESC_ESPECIE_SERIE IS NULL)
								--AND (RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
								AND A.ECF <> 0
								AND A.SERIE_NF <> 'D1'
								AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
								AND A.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
							GROUP BY A.DATA_FISCAL, A.CF_INICIAL,
							A.CODIGO_FILIAL, A.TERMINAL, A.ID_EQUIPAMENTO, A.ECF, A.SERIE_NF) AS LCFT_ISS
							ON 	LCF.CODIGO_FILIAL 		= LCFT_ISS.CODIGO_FILIAL 		AND
								LCF.TERMINAL 			= LCFT_ISS.TERMINAL 			AND
								LCF.DATA_FISCAL 		= LCFT_ISS.DATA_FISCAL			AND
								LCF.ID_EQUIPAMENTO		= LCFT_ISS.ID_EQUIPAMENTO		AND
								LCF.ECF					= LCFT_ISS.ECF					AND
								LCF.SERIE_NF			= LCFT_ISS.SERIE_NF				AND
								LCF.CF_INICIAL			= LCFT_ISS.CF_INICIAL

			LEFT JOIN (SELECT A.DATA_FISCAL, A.CF_INICIAL,
							A.CODIGO_FILIAL, A.TERMINAL, A.ID_EQUIPAMENTO, A.ECF, A.SERIE_NF,
							VL_BC_ICMS = SUM(LCFT.TOTAL_TARIFA),
							VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END),
							VL_ISEN = SUM(LCFT.VALOR_ISENTO)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO AS FIL (NOLOCK)
								ON A.CODIGO_FILIAL = FIL.CODIGO_FILIAL
							JOIN FILIAIS (NOLOCK)
								ON FILIAIS.FILIAL = FIL.FILIAL
							LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
								ON A.ESPECIE_SERIE = ESP.ESPECIE_SERIE
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO = 1
								AND (RTRIM(ESP.DESC_ESPECIE_SERIE) = 'CF' OR  RTRIM(ESP.DESC_ESPECIE_SERIE) = 'ECF' OR ESP.DESC_ESPECIE_SERIE IS NULL)
								--AND (RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
								AND A.ECF <> 0
								AND A.SERIE_NF <> 'D1'
								AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
								AND A.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
							GROUP BY A.DATA_FISCAL, A.CF_INICIAL,
							A.CODIGO_FILIAL, A.TERMINAL, A.ID_EQUIPAMENTO, A.ECF, A.SERIE_NF) AS LCFT_ICMS
							ON 	LCF.CODIGO_FILIAL 		= LCFT_ICMS.CODIGO_FILIAL 		AND
								LCF.TERMINAL 			= LCFT_ICMS.TERMINAL 			AND
								LCF.DATA_FISCAL 		= LCFT_ICMS.DATA_FISCAL			AND
								LCF.ID_EQUIPAMENTO		= LCFT_ICMS.ID_EQUIPAMENTO		AND
								LCF.ECF					= LCFT_ICMS.ECF					AND
								LCF.SERIE_NF			= LCFT_ICMS.SERIE_NF			AND
								LCF.CF_INICIAL			= LCFT_ICMS.CF_INICIAL
		WHERE 
			(RTRIM(ESP.DESC_ESPECIE_SERIE) = 'CF' OR  RTRIM(ESP.DESC_ESPECIE_SERIE) = 'ECF' OR ESP.DESC_ESPECIE_SERIE IS NULL)
			--(RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
			AND LCF.ECF <> 0
			AND LCF.SERIE_NF <> 'D1'
			AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
			AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY LCF.NR_MAPA_RESUMO, LCF.DATA_FISCAL, LCF.OBS

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E085
		SELECT
			REG				=	'|E085|',
			LCF_TOTAL.TOTAL_CANCELADO,
			LCF_TOTAL.TOTAL_DESCONTO,
			LCF_TOTAL.TOTAL_BRUTO,
			LCF_TOTAL.TOTAL_ISENTO,
			LCF_TOTAL.TOTAL_NAO_TRIBUTAVEL,
			LCF_TOTAL.TOTAL_SUBSTITUICAO,
			LCFT_ISS.VL_ISSQN,
			LCFT_ICMS.CFOP,
			LCFT_ICMS.VL_BC_ICMS, LCFT_ICMS.ALIQ_ICMS, LCFT_ICMS.VL_ICMS, LCFT_ICMS.VL_ISEN,
			NUM_MR_ECF		=	ISNULL(RTRIM(LTRIM(STR(LCF.NR_MAPA_RESUMO))), '0') + '|',
			DT_MR_ECF		=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), LCF.DATA_FISCAL, 103), '/', ''))), '') + '|',
			IND_OBS			=	CASE WHEN RTRIM(LTRIM(ISNULL(LCF.OBS, ''))) = '' THEN '0|' ELSE '1|' END
		FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
			LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
				ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
			JOIN LOJAS_VAREJO AS FIL (NOLOCK)
				ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
			JOIN FILIAIS (NOLOCK)
				ON FILIAIS.FILIAL = FIL.FILIAL
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
							VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
							LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
								A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
								A.TERMINAL 				= LCFT.TERMINAL 			AND
								A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
								A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
								A.ECF					= LCFT.ECF					AND
								A.SERIE_NF				= LCFT.SERIE_NF				AND
								A.CF_INICIAL			= LCFT.CF_INICIAL
							JOIN LOJAS_VAREJO G ON
								A.CODIGO_FILIAL	= G.CODIGO_FILIAL
							JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
								G.FILIAL 			= D.NOME_CLIFOR
							CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
							LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
								C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
							LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO IN (14, 22)
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
			ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
				AND LCF.TERMINAL = LCFT_ISS.TERMINAL
				AND LCF.ECF = LCFT_ISS.ECF
				AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
				AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
				AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
				AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
			LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
							LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.LEGENDA_TARIFA, LCFT.ID_IMPOSTO,
							CFOP = LCFT.CODIGO_FISCAL_OPERACAO,
							ALIQ_ICMS = LCFT.PER_TARIFA,
							VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
												ELSE
													CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
												END),
							VL_ISEN = SUM(LCFT.VALOR_ISENTO),
							VL_BC_ICMS = CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ISNULL(LCFT.TOTAL_TARIFA,0)+
												CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN ISNULL(LCFT.TOTAL_TARIFA,0) ELSE
													CASE WHEN ISNULL(A.NOTA_CANCELADA,0) = 0 THEN ISNULL(CASE WHEN (A.TOTAL_ISENTO+A.TOTAL_NAO_TRIBUTAVEL+A.TOTAL_SUBSTITUICAO)-Z.VALOR_OUTROS_TARIFAS-Z.VALOR_ISENTO_TARIFAS < 0 THEN 0.00 ELSE (A.TOTAL_ISENTO+A.TOTAL_NAO_TRIBUTAVEL+A.TOTAL_SUBSTITUICAO)-Z.VALOR_OUTROS_TARIFAS-Z.VALOR_ISENTO_TARIFAS END *((100*(CASE WHEN LCFT.TOTAL_TARIFA = 0 THEN 1 ELSE LCFT.TOTAL_TARIFA END)/
																CASE WHEN (SELECT SUM(TOTAL_TARIFA)
															FROM LOJA_CONTROLE_FISCAL_TARIFAS (NOLOCK)
															WHERE CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
																		TERMINAL 		= LCFT.TERMINAL 			AND
																		DATA_FISCAL 	= LCFT.DATA_FISCAL			AND
																		ID_EQUIPAMENTO	= LCFT.ID_EQUIPAMENTO		AND
																		ECF				= LCFT.ECF					AND
																		SERIE_NF		= LCFT.SERIE_NF				AND
																		CF_INICIAL		= LCFT.CF_INICIAL			AND
																		ID_IMPOSTO		= 1) = 0 THEN 1 ELSE
															(SELECT SUM(TOTAL_TARIFA)
															FROM LOJA_CONTROLE_FISCAL_TARIFAS (NOLOCK)
															WHERE CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
																		TERMINAL 		= LCFT.TERMINAL 			AND
																		DATA_FISCAL 	= LCFT.DATA_FISCAL			AND
																		ID_EQUIPAMENTO	= LCFT.ID_EQUIPAMENTO		AND
																		ECF				= LCFT.ECF					AND
																		SERIE_NF		= LCFT.SERIE_NF				AND
																		CF_INICIAL		= LCFT.CF_INICIAL			AND
																		ID_IMPOSTO		= 1	) END
															)/100),0) ELSE 0.00 END
												END ,2,2) END END+ISNULL(LCFT.VALOR_OUTROS,0.00)+ISNULL(LCFT.VALOR_ISENTO,0.00)
							FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
								LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
									A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
									A.TERMINAL 				= LCFT.TERMINAL 			AND
									A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
									A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
									A.ECF					= LCFT.ECF					AND
									A.SERIE_NF				= LCFT.SERIE_NF				AND
									A.CF_INICIAL			= LCFT.CF_INICIAL
								LEFT JOIN (SELECT Y.CODIGO_FILIAL,
														Y.TERMINAL,
														Y.DATA_FISCAL,
														Y.ID_EQUIPAMENTO,
														Y.ECF,
														Y.SERIE_NF,
														Y.CF_INICIAL,
														VALOR_ISENTO_TARIFAS		= SUM(ISNULL(VALOR_ISENTO,0.00)),
														VALOR_OUTROS_TARIFAS		= SUM(ISNULL(VALOR_OUTROS,0.00))
													FROM LOJA_CONTROLE_FISCAL_TARIFAS Y
												GROUP BY Y.CODIGO_FILIAL, Y.TERMINAL, Y.DATA_FISCAL, Y.ID_EQUIPAMENTO, Y.ECF, Y.SERIE_NF,	Y.CF_INICIAL) Z ON
									A.CODIGO_FILIAL 		= Z.CODIGO_FILIAL 		AND
									A.TERMINAL 				= Z.TERMINAL 			AND
									A.DATA_FISCAL 			= Z.DATA_FISCAL			AND
									A.ID_EQUIPAMENTO		= Z.ID_EQUIPAMENTO		AND
									A.ECF					= Z.ECF					AND
									A.SERIE_NF				= Z.SERIE_NF			AND
									A.CF_INICIAL			= Z.CF_INICIAL
								JOIN LOJAS_VAREJO G ON
									A.CODIGO_FILIAL	= G.CODIGO_FILIAL
								JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
									G.FILIAL 			= D.NOME_CLIFOR
								CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
								LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
									C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
								LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
							WHERE LCFT.ID_IMPOSTO  = 1
							GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
								LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.CODIGO_FISCAL_OPERACAO,
								LCFT.PER_TARIFA, L.LX_CODIGO_APURACAO, LCFT.TOTAL_TARIFA,
								D.INDICADOR_FISCAL_TERCEIRO, A.TOTAL_ISENTO, A.TOTAL_NAO_TRIBUTAVEL,
								A.TOTAL_SUBSTITUICAO, Z.VALOR_OUTROS_TARIFAS, Z.VALOR_ISENTO_TARIFAS,
								A.NOTA_CANCELADA, LCFT.VALOR_OUTROS, LCFT.VALOR_ISENTO, LCFT.LEGENDA_TARIFA, LCFT.ID_IMPOSTO) AS LCFT_ICMS
			ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
				AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
				AND LCF.ECF = LCFT_ICMS.ECF
				AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
				AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
				AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
				AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
			LEFT JOIN (SELECT A.CODIGO_FILIAL, A.TERMINAL, A.DATA_FISCAL, A.LEGENDA_TARIFA, A.ECF, A.ID_EQUIPAMENTO, A.CF_INICIAL, A.SERIE_NF, A.ID_IMPOSTO,
							 TOTAL_DESCONTO       = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_DESCONTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_DESCONTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
							 TOTAL_CANCELADO      = ROUND(CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN (TOTAL_CANCELADO / QTD) ELSE (TOTAL_CANCELADO * (TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA ) / 100.00 END, 2, 2),
--							 TOTAL_CANCELADO      = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_CANCELADO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_CANCELADO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
							 TOTAL_BRUTO          = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_BRUTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_BRUTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
							 TOTAL_ISENTO         = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_ISENTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_ISENTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
							 TOTAL_NAO_TRIBUTAVEL = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_NAO_TRIBUTAVEL / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_NAO_TRIBUTAVEL * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
							 TOTAL_SUBSTITUICAO   = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_SUBSTITUICAO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_SUBSTITUICAO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END
						FROM LOJA_CONTROLE_FISCAL_TARIFAS A
							  JOIN (SELECT A.CODIGO_FILIAL, A.TERMINAL, A.ECF, A.ID_EQUIPAMENTO, A.DATA_FISCAL, A.CF_INICIAL, A.SERIE_NF,
									   SUM(TOTAL_TARIFA) AS SOMA_TOTAL_TARIFA, COUNT(*) AS QTD
									FROM LOJA_CONTROLE_FISCAL_TARIFAS A
									WHERE ID_IMPOSTO = 1
									GROUP BY A.CODIGO_FILIAL, A.TERMINAL, A.ECF, A.ID_EQUIPAMENTO, A.DATA_FISCAL, A.CF_INICIAL, A.SERIE_NF ) B ON
									 A.CODIGO_FILIAL      = B.CODIGO_FILIAL    AND
									 A.TERMINAL           = B.TERMINAL         AND
									 A.ECF                = B.ECF              AND
									 A.ID_EQUIPAMENTO     = B.ID_EQUIPAMENTO   AND
									 A.DATA_FISCAL        = B.DATA_FISCAL      AND
									 A.CF_INICIAL         = B.CF_INICIAL       AND
									 A.SERIE_NF           = B.SERIE_NF
								  JOIN LOJA_CONTROLE_FISCAL C ON
									 A.CODIGO_FILIAL      = C.CODIGO_FILIAL    AND
									 A.TERMINAL           = C.TERMINAL         AND
									 A.ECF                = C.ECF              AND
									 A.ID_EQUIPAMENTO     = C.ID_EQUIPAMENTO   AND
									 A.DATA_FISCAL        = C.DATA_FISCAL      AND
									 A.CF_INICIAL         = C.CF_INICIAL       AND
									 A.SERIE_NF           = C.SERIE_NF
								WHERE A.ID_IMPOSTO      = 1) AS LCF_TOTAL
			ON LCF_TOTAL.CODIGO_FILIAL		=	LCFT_ICMS.CODIGO_FILIAL		AND
				LCF_TOTAL.TERMINAL			=	LCFT_ICMS.TERMINAL			AND
				LCF_TOTAL.DATA_FISCAL		=	LCFT_ICMS.DATA_FISCAL		AND
				LCF_TOTAL.LEGENDA_TARIFA	=	LCFT_ICMS.LEGENDA_TARIFA	AND
				LCF_TOTAL.ECF					=	LCFT_ICMS.ECF			AND
				LCF_TOTAL.ID_EQUIPAMENTO	=	LCFT_ICMS.ID_EQUIPAMENTO	AND
				LCF_TOTAL.CF_INICIAL			=	LCFT_ICMS.CF_INICIAL	AND
				LCF_TOTAL.SERIE_NF			=	LCFT_ICMS.SERIE_NF			AND
				LCF_TOTAL.ID_IMPOSTO			=	LCFT_ICMS.ID_IMPOSTO
		WHERE
			(RTRIM(ESP.DESC_ESPECIE_SERIE) = 'CF' OR  RTRIM(ESP.DESC_ESPECIE_SERIE) = 'ECF' OR ESP.DESC_ESPECIE_SERIE IS NULL)
			--(RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
			AND LCF.ECF <> 0
			AND LCF.SERIE_NF <> 'D1'
			AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
			AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E100
			SELECT
				REG				=	'|E100|',
				IND_OPER		=	'0|',
				IND_EMIT		=	CASE WHEN A.NF_PROPRIA_EMITIDA = 0 THEN '1' ELSE '0' END + '|',
				COD_PART		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.COD_CLIFOR)), '') END + '|',
				COD_MOD			=	'06|',
				COD_SIT			=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER				=	ISNULL(RTRIM(LTRIM(S.COD_SERIE_SINTEGRA)), '') + '|',
				SUB				=	'|',
				COD_CONS		=	'00|',
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
				DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
				DT_E_S			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.RECEBIMENTO, 103), '/', ''))), '') + '|',
				QTD_DOC			=	'00|',
				QTD_CANC		=	'|',
				VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_CONTABIL), 15, 2))), '') + '|',
				VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.BASE_IMPOSTO), 15, 2))), '') + '|',
				VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO), 15, 2))), '') + '|',
				VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ISENTO), 15, 2))), '') + '|',
				VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_OUTROS), 15, 2))), '') + '|',
				COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				SERIE			=	A.SERIE_NF_ENTRADA,
				A.COD_CLIFOR, A.COD_FILIAL
			FROM LF_REGISTRO_ENTRADA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF_ENTRADA		=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
			WHERE  A.CODIGO_FISCAL_OPERACAO IN ('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
												'1256', '2256', '1257', '2257',
												'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
												'1306', '2306')
				AND	A.ID_IMPOSTO	= 1
				AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY A.NF_PROPRIA_EMITIDA, A.COD_CLIFOR, A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_ENTRADA, A.EMISSAO,
				A.CTB_LANCAMENTO, A.RECEBIMENTO, A.CODIGO_FISCAL_OPERACAO, A.OBS, A.SERIE_NF_ENTRADA, A.COD_CLIFOR, A.COD_FILIAL

			UNION ALL

			SELECT 	DISTINCT
				REG				=	'|E100|',
				IND_OPER		=	'1|',
				IND_EMIT		=	'0|',
				COD_PART		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.COD_CLIFOR)), '') END + '|',
				COD_MOD			=	'06|',
				COD_SIT			=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
				SER				=	ISNULL(RTRIM(LTRIM(S.COD_SERIE_SINTEGRA)), '') + '|',
				SUB				=	'|',
				COD_CONS		=	'00|',
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
				NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
				DT_E_S			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_SAIDA, 103), '/', ''))), '') + '|',
				QTD_DOC			=	'00|',
				QTD_CANC		=	'0|',
				VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_CONTABIL), 15, 2))), '') + '|',
				VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.BASE_IMPOSTO), 15, 2))), '') + '|',
				VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO), 15, 2))), '') + '|',
				VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ISENTO), 15, 2))), '') + '|',
				VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_OUTROS), 15, 2))), '') + '|',
				COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|',
				SERIE			=	A.SERIE_NF,
				A.COD_CLIFOR, A.COD_FILIAL
			FROM LF_REGISTRO_SAIDA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF			=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE	=	S.ESPECIE_SERIE
			WHERE  A.CODIGO_FISCAL_OPERACAO IN ('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
												'5256', '6256', '5257', '6257', '5258', '6258',
												'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
												'5306', '6306', '5307', '6307')
				AND ID_IMPOSTO = 1
				AND A.ESPECIE NOT IN ('ECF', 'CF')
				AND A.SERIE_NF <> 'D1'
				AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY A.COD_CLIFOR, A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_SAIDA, A.EMISSAO, A.CTB_LANCAMENTO,
					A.DATA_SAIDA, A.CODIGO_FISCAL_OPERACAO, A.OBS, A.SERIE_NF, A.COD_CLIFOR, A.COD_FILIAL

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E105
			SELECT
				REG				=	'|E105|',
				CFOP			=	ISNULL(RTRIM(LTRIM(A.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_CONT_P		=	SUM(A.VALOR_CONTABIL),
				VL_BC_ICMS_P	=	SUM(A.BASE_IMPOSTO),
				ALIQ_ICMS		=	MAX(A.TAXA_IMPOSTO),
				VL_ICMS_P		=	SUM(A.VALOR_IMPOSTO),
				VL_ISNT_ICMS_P	=	SUM(A.VALOR_IMPOSTO_ISENTO),
				VL_OUT_ICMS_P	=	SUM(A.VALOR_IMPOSTO_OUTROS),
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
				SERIE			=	A.SERIE_NF_ENTRADA,
				A.COD_CLIFOR, A.COD_FILIAL
			FROM LF_REGISTRO_ENTRADA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF_ENTRADA		=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
			WHERE  A.CODIGO_FISCAL_OPERACAO IN ('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
												'1256', '2256', '1257', '2257',
												'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
												'1306', '2306')
				AND	A.ID_IMPOSTO	=	1
				AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO		BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY A.CODIGO_FISCAL_OPERACAO, A.NF_ENTRADA, A.SERIE_NF_ENTRADA, A.COD_CLIFOR, A.COD_FILIAL

			UNION ALL

			SELECT 	DISTINCT
				REG				=	'|E105|',
				CFOP			=	ISNULL(RTRIM(LTRIM(A.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_CONT_P		=	SUM(A.VALOR_CONTABIL),
				VL_BC_ICMS_P	=	SUM(A.BASE_IMPOSTO),
				ALIQ_ICMS		=	MAX(A.TAXA_IMPOSTO),
				VL_ICMS_P		=	SUM(A.VALOR_IMPOSTO),
				VL_ISNT_ICMS_P	=	SUM(A.VALOR_IMPOSTO_ISENTO),
				VL_OUT_ICMS_P	=	SUM(A.VALOR_IMPOSTO_OUTROS),
				NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
				SERIE			=	A.SERIE_NF,
				A.COD_CLIFOR, A.COD_FILIAL
			FROM LF_REGISTRO_SAIDA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF			=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE	=	S.ESPECIE_SERIE
			WHERE  A.CODIGO_FISCAL_OPERACAO IN ('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
												'5256', '6256', '5257', '6257', '5258', '6258',
												'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
												'5306', '6306', '5307', '6307')
				AND A.ID_IMPOSTO = 1
				AND A.ESPECIE NOT IN ('ECF', 'CF')
				AND A.SERIE_NF <> 'D1'
				AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
				AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
			GROUP BY A.CODIGO_FISCAL_OPERACAO, A.NF_SAIDA, A.SERIE_NF, A.COD_CLIFOR, A.COD_FILIAL

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E120
		SELECT
			REG				=	'|E120|',
			IND_OPER		=	'0|',
			IND_EMIT		=	CASE WHEN A.NF_PROPRIA_EMITIDA = 0 THEN '1' ELSE '0' END + '|',
			COD_PART		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.COD_CLIFOR)), '') END + '|',
			COD_MOD			=	'07|',
			COD_SIT			=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
			SER				=	ISNULL(RTRIM(LTRIM(S.COD_SERIE_SINTEGRA)), '') + '|',
			SUB				=	'|',
			NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_ENTRADA)), '') + '|',
			DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
			NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
			DT_A_P			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.RECEBIMENTO, 103), '/', ''))), '') + '|',
			CFOP			=	ISNULL(RTRIM(LTRIM(A.CODIGO_FISCAL_OPERACAO)), '') + '|',
			VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_CONTABIL), 15, 2))), '') + '|',
			VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.BASE_IMPOSTO), 15, 2))), '') + '|',
			ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.TAXA_IMPOSTO), 15, 2))), '') + '|',
			VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO), 15, 2))), '') + '|',
			VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ISENTO), 15, 2))), '') + '|',
			VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_OUTROS), 15, 2))), '') + '|',
			COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|'
			FROM LF_REGISTRO_ENTRADA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF_ENTRADA		=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
		WHERE  A.CODIGO_FISCAL_OPERACAO IN ('1350', '1352', '1353', '1354', '1355', '1356', '1932',
											'2350', '2352', '2353', '2354', '2355', '2356', '2932',
											'3350', '3352', '3353', '3354', '3355', '3356')
			AND	A.ID_IMPOSTO	=	1
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.RECEBIMENTO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY A.NF_PROPRIA_EMITIDA, A.COD_CLIFOR, A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_ENTRADA, A.EMISSAO,
			A.CTB_LANCAMENTO, A.RECEBIMENTO, A.CODIGO_FISCAL_OPERACAO, A.OBS

		UNION ALL

		SELECT 	DISTINCT
			REG				=	'|E120|',
			IND_OPER		=	'1|',
			IND_EMIT		=	'0|',
			COD_PART		=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '' ELSE ISNULL(RTRIM(LTRIM(A.COD_CLIFOR)), '') END + '|',
			COD_MOD			=	'07|',
			COD_SIT			=	CASE WHEN A.NOTA_CANCELADA > 0 THEN '02' ELSE '00' END + '|',
			SER				=	ISNULL(RTRIM(LTRIM(S.COD_SERIE_SINTEGRA)), '') + '|',
			SUB				=	'|',
			NUM_DOC			=	ISNULL(RTRIM(LTRIM(A.NF_SAIDA)), '') + '|',
			DT_DOC			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.EMISSAO, 103), '/', ''))), '') + '|',
			NUM_LCTO		=	ISNULL(RTRIM(LTRIM(A.CTB_LANCAMENTO)), '') + '|',
			DT_A_P			=	ISNULL(RTRIM(LTRIM(REPLACE(CONVERT(CHAR(10), A.DATA_SAIDA, 103), '/', ''))), '') + '|',
			CFOP			=	ISNULL(RTRIM(LTRIM(A.CODIGO_FISCAL_OPERACAO)), '') + '|',
			VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_CONTABIL), 15, 2))), '') + '|',
			VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.BASE_IMPOSTO), 15, 2))), '') + '|',
			ALIQ_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.TAXA_IMPOSTO), 15, 2))), '') + '|',
			VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO), 15, 2))), '') + '|',
			VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ISENTO), 15, 2))), '') + '|',
			VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_OUTROS), 15, 2))), '') + '|',
			COD_INF_OBS		=	ISNULL(RTRIM(LTRIM(A.OBS)), '') + '|'
			FROM LF_REGISTRO_SAIDA A
				LEFT JOIN SERIES_NF AS S ON
					A.SERIE_NF			=	S.SERIE_NF
				LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
					ES.ESPECIE_SERIE	=	S.ESPECIE_SERIE
		WHERE  A.CODIGO_FISCAL_OPERACAO IN ('5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
											'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
											'7350', '7358')
			AND ID_IMPOSTO = 1
			AND A.ESPECIE NOT IN ('ECF', 'CF')
			AND A.SERIE_NF <> 'D1'
			AND A.COD_FILIAL	=	'<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.EMISSAO	BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY A.COD_CLIFOR, A.NOTA_CANCELADA, S.COD_SERIE_SINTEGRA, A.NF_SAIDA, A.EMISSAO, A.CTB_LANCAMENTO,
				A.DATA_SAIDA, A.CODIGO_FISCAL_OPERACAO, A.OBS

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E310
			SELECT
				REG				=	'|E310|',
				CFOP			=	ISNULL(RTRIM(LTRIM(A.CODIGO_FISCAL_OPERACAO)), '') + '|',
				VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_CONTABIL), 15, 2))), '0.00') + '|',
				VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.BASE_IMPOSTO_ICMS), 15, 2))), '0.00') + '|',
				VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ICMS), 15, 2))), '0.00') + '|',
				VL_ST			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ST), 15, 2))), '0.00') + '|',
				VL_COMPL		=	'0.00|',
				VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ISENTO_ICMS), 15, 2))), '0.00') + '|',
				VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_OUTROS_ICMS), 15, 2))), '0.00') + '|'
			FROM (
				SELECT
					CODIGO_FISCAL_OPERACAO		=	LFE.CODIGO_FISCAL_OPERACAO,
					VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 12 THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_ENTRADA LFE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306',
														'1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356',
														'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
														'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
														'1932', '2932')							AND
					LFE.ID_IMPOSTO				IN		(1, 12)									AND
					LFE.NOTA_CANCELADA			=		0										AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFE.CODIGO_FISCAL_OPERACAO

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LFS.CODIGO_FISCAL_OPERACAO,
					VALOR_CONTABIL				=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 12 THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_SAIDA AS LFS
				WHERE
					LFS.CODIGO_FISCAL_OPERACAO	NOT IN	('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
														'5256', '6256', '5257', '6257', '5258', '6258',
														'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
														'5306', '6306', '5307', '6307',
														'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
														'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
														'7350', '7358',
														'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
														'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
														'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
														'5932', '6932')							AND
					LFS.ID_IMPOSTO				IN		(1, 12)									AND
					LFS.NOTA_CANCELADA			=		0										AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFS.CODIGO_FISCAL_OPERACAO

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LFS.CODIGO_FISCAL_OPERACAO,
					VALOR_CONTABIL				=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_SAIDA LFS
				WHERE
					LFS.SERIE_NF			=		'D1'									AND
					LFS.NOTA_CANCELADA		=		0										AND
					LFS.ID_IMPOSTO			=		1										AND
					LFS.COD_FILIAL			=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFS.CODIGO_FISCAL_OPERACAO

				UNION ALL

				SELECT
					LCFT_ICMS.CFOP AS CODIGO_FISCAL_OPERACAO, LCFT_ICMS.VL_BC_ICMS AS VALOR_CONTABIL, LCFT_ICMS.VL_BC_ICMS AS BASE_IMPOSTO_ICMS,
					LCFT_ICMS.VL_ICMS AS VALOR_IMPOSTO_ICMS, 0 AS VALOR_IMPOSTO_ST,
					LCFT_ICMS.VL_ISEN AS VALOR_IMPOSTO_ISENTO_ICMS, 0 AS VALOR_IMPOSTO_OUTROS_ICMS
				FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
					LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
						ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
					JOIN LOJAS_VAREJO AS FIL (NOLOCK)
						ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
					JOIN FILIAIS (NOLOCK)
						ON FILIAIS.FILIAL = FIL.FILIAL
					LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
									LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
									VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
														ELSE
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
														END)
									FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
									LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
										A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
										A.TERMINAL 				= LCFT.TERMINAL 			AND
										A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
										A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
										A.ECF					= LCFT.ECF					AND
										A.SERIE_NF				= LCFT.SERIE_NF				AND
										A.CF_INICIAL			= LCFT.CF_INICIAL
									JOIN LOJAS_VAREJO G ON
										A.CODIGO_FILIAL	= G.CODIGO_FILIAL
									JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
										G.FILIAL 			= D.NOME_CLIFOR
									CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
									LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
										C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
									LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
									WHERE LCFT.ID_IMPOSTO IN (14, 22)
									GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
									LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
					ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
						AND LCF.TERMINAL = LCFT_ISS.TERMINAL
						AND LCF.ECF = LCFT_ISS.ECF
						AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
						AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
						AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
						AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
					LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
									LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.LEGENDA_TARIFA, LCFT.ID_IMPOSTO,
									CFOP = LCFT.CODIGO_FISCAL_OPERACAO,
									ALIQ_ICMS = LCFT.PER_TARIFA,
									VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
														ELSE
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
														END),
									VL_ISEN = SUM(LCFT.VALOR_ISENTO),
									VL_BC_ICMS = CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ISNULL(LCFT.TOTAL_TARIFA,0)+
														CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN ISNULL(LCFT.TOTAL_TARIFA,0) ELSE
															CASE WHEN ISNULL(A.NOTA_CANCELADA,0) = 0 THEN ISNULL(CASE WHEN (A.TOTAL_ISENTO+A.TOTAL_NAO_TRIBUTAVEL+A.TOTAL_SUBSTITUICAO)-Z.VALOR_OUTROS_TARIFAS-Z.VALOR_ISENTO_TARIFAS < 0 THEN 0.00 ELSE (A.TOTAL_ISENTO+A.TOTAL_NAO_TRIBUTAVEL+A.TOTAL_SUBSTITUICAO)-Z.VALOR_OUTROS_TARIFAS-Z.VALOR_ISENTO_TARIFAS END *((100*(CASE WHEN LCFT.TOTAL_TARIFA = 0 THEN 1 ELSE LCFT.TOTAL_TARIFA END)/
																		CASE WHEN (SELECT SUM(TOTAL_TARIFA)
																	FROM LOJA_CONTROLE_FISCAL_TARIFAS (NOLOCK)
																	WHERE CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
																				TERMINAL 		= LCFT.TERMINAL 			AND
																				DATA_FISCAL 	= LCFT.DATA_FISCAL			AND
																				ID_EQUIPAMENTO	= LCFT.ID_EQUIPAMENTO		AND
																				ECF				= LCFT.ECF					AND
																				SERIE_NF		= LCFT.SERIE_NF				AND
																				CF_INICIAL		= LCFT.CF_INICIAL			AND
																				ID_IMPOSTO		= 1) = 0 THEN 1 ELSE
																	(SELECT SUM(TOTAL_TARIFA)
																	FROM LOJA_CONTROLE_FISCAL_TARIFAS (NOLOCK)
																	WHERE CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
																				TERMINAL 		= LCFT.TERMINAL 			AND
																				DATA_FISCAL 	= LCFT.DATA_FISCAL			AND
																				ID_EQUIPAMENTO	= LCFT.ID_EQUIPAMENTO		AND
																				ECF				= LCFT.ECF					AND
																				SERIE_NF		= LCFT.SERIE_NF				AND
																				CF_INICIAL		= LCFT.CF_INICIAL			AND
																				ID_IMPOSTO		= 1	) END
																	)/100),0) ELSE 0.00 END
														END ,2,2) END END+ISNULL(LCFT.VALOR_OUTROS,0.00)+ISNULL(LCFT.VALOR_ISENTO,0.00)
									FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
										LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
											A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
											A.TERMINAL 				= LCFT.TERMINAL 			AND
											A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
											A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
											A.ECF					= LCFT.ECF					AND
											A.SERIE_NF				= LCFT.SERIE_NF				AND
											A.CF_INICIAL			= LCFT.CF_INICIAL
										LEFT JOIN (SELECT Y.CODIGO_FILIAL,
																Y.TERMINAL,
																Y.DATA_FISCAL,
																Y.ID_EQUIPAMENTO,
																Y.ECF,
																Y.SERIE_NF,
																Y.CF_INICIAL,
																VALOR_ISENTO_TARIFAS		= SUM(ISNULL(VALOR_ISENTO,0.00)),
																VALOR_OUTROS_TARIFAS		= SUM(ISNULL(VALOR_OUTROS,0.00))
															FROM LOJA_CONTROLE_FISCAL_TARIFAS Y
														GROUP BY Y.CODIGO_FILIAL, Y.TERMINAL, Y.DATA_FISCAL, Y.ID_EQUIPAMENTO, Y.ECF, Y.SERIE_NF,	Y.CF_INICIAL) Z ON
											A.CODIGO_FILIAL 		= Z.CODIGO_FILIAL 		AND
											A.TERMINAL 				= Z.TERMINAL 			AND
											A.DATA_FISCAL 			= Z.DATA_FISCAL			AND
											A.ID_EQUIPAMENTO		= Z.ID_EQUIPAMENTO		AND
											A.ECF					= Z.ECF					AND
											A.SERIE_NF				= Z.SERIE_NF			AND
											A.CF_INICIAL			= Z.CF_INICIAL
										JOIN LOJAS_VAREJO G ON
											A.CODIGO_FILIAL	= G.CODIGO_FILIAL
										JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
											G.FILIAL 			= D.NOME_CLIFOR
										CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
										LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
											C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
										LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
									WHERE LCFT.ID_IMPOSTO  = 1
									GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
										LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.CODIGO_FISCAL_OPERACAO,
										LCFT.PER_TARIFA, L.LX_CODIGO_APURACAO, LCFT.TOTAL_TARIFA,
										D.INDICADOR_FISCAL_TERCEIRO, A.TOTAL_ISENTO, A.TOTAL_NAO_TRIBUTAVEL,
										A.TOTAL_SUBSTITUICAO, Z.VALOR_OUTROS_TARIFAS, Z.VALOR_ISENTO_TARIFAS,
										A.NOTA_CANCELADA, LCFT.VALOR_OUTROS, LCFT.VALOR_ISENTO, LCFT.LEGENDA_TARIFA, LCFT.ID_IMPOSTO) AS LCFT_ICMS
					ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
						AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
						AND LCF.ECF = LCFT_ICMS.ECF
						AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
						AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
						AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
						AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
					LEFT JOIN (SELECT A.CODIGO_FILIAL, A.TERMINAL, A.DATA_FISCAL, A.LEGENDA_TARIFA, A.ECF, A.ID_EQUIPAMENTO, A.CF_INICIAL, A.SERIE_NF, A.ID_IMPOSTO,
									 TOTAL_DESCONTO       = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_DESCONTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_DESCONTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_CANCELADO      = ROUND(CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN (TOTAL_CANCELADO / QTD) ELSE (TOTAL_CANCELADO * (TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA ) / 100.00 END, 2, 2),
		--							 TOTAL_CANCELADO      = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_CANCELADO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_CANCELADO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_BRUTO          = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_BRUTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_BRUTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_ISENTO         = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_ISENTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_ISENTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_NAO_TRIBUTAVEL = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_NAO_TRIBUTAVEL / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_NAO_TRIBUTAVEL * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_SUBSTITUICAO   = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_SUBSTITUICAO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_SUBSTITUICAO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END
								FROM LOJA_CONTROLE_FISCAL_TARIFAS A
									  JOIN (SELECT A.CODIGO_FILIAL, A.TERMINAL, A.ECF, A.ID_EQUIPAMENTO, A.DATA_FISCAL, A.CF_INICIAL, A.SERIE_NF,
											   SUM(TOTAL_TARIFA) AS SOMA_TOTAL_TARIFA, COUNT(*) AS QTD
											FROM LOJA_CONTROLE_FISCAL_TARIFAS A
											WHERE ID_IMPOSTO = 1
											GROUP BY A.CODIGO_FILIAL, A.TERMINAL, A.ECF, A.ID_EQUIPAMENTO, A.DATA_FISCAL, A.CF_INICIAL, A.SERIE_NF ) B ON
											 A.CODIGO_FILIAL      = B.CODIGO_FILIAL    AND
											 A.TERMINAL           = B.TERMINAL         AND
											 A.ECF                = B.ECF              AND
											 A.ID_EQUIPAMENTO     = B.ID_EQUIPAMENTO   AND
											 A.DATA_FISCAL        = B.DATA_FISCAL      AND
											 A.CF_INICIAL         = B.CF_INICIAL       AND
											 A.SERIE_NF           = B.SERIE_NF
										  JOIN LOJA_CONTROLE_FISCAL C ON
											 A.CODIGO_FILIAL      = C.CODIGO_FILIAL    AND
											 A.TERMINAL           = C.TERMINAL         AND
											 A.ECF                = C.ECF              AND
											 A.ID_EQUIPAMENTO     = C.ID_EQUIPAMENTO   AND
											 A.DATA_FISCAL        = C.DATA_FISCAL      AND
											 A.CF_INICIAL         = C.CF_INICIAL       AND
											 A.SERIE_NF           = C.SERIE_NF
										WHERE A.ID_IMPOSTO      = 1) AS LCF_TOTAL
					ON LCF_TOTAL.CODIGO_FILIAL		=	LCFT_ICMS.CODIGO_FILIAL		AND
						LCF_TOTAL.TERMINAL			=	LCFT_ICMS.TERMINAL			AND
						LCF_TOTAL.DATA_FISCAL		=	LCFT_ICMS.DATA_FISCAL		AND
						LCF_TOTAL.LEGENDA_TARIFA	=	LCFT_ICMS.LEGENDA_TARIFA	AND
						LCF_TOTAL.ECF					=	LCFT_ICMS.ECF			AND
						LCF_TOTAL.ID_EQUIPAMENTO	=	LCFT_ICMS.ID_EQUIPAMENTO	AND
						LCF_TOTAL.CF_INICIAL			=	LCFT_ICMS.CF_INICIAL	AND
						LCF_TOTAL.SERIE_NF			=	LCFT_ICMS.SERIE_NF			AND
						LCF_TOTAL.ID_IMPOSTO			=	LCFT_ICMS.ID_IMPOSTO
				WHERE 
					(RTRIM(ESP.DESC_ESPECIE_SERIE) = 'CF' OR  RTRIM(ESP.DESC_ESPECIE_SERIE) = 'ECF' OR ESP.DESC_ESPECIE_SERIE IS NULL)
					--(RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
					AND LCF.ECF <> 0
					AND LCF.SERIE_NF <> 'D1'
					AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
					AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LFE.CODIGO_FISCAL_OPERACAO,
					VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_ENTRADA LFE
					LEFT JOIN SERIES_NF AS S ON
						LFE.SERIE_NF_ENTRADA		=	S.SERIE_NF
					LEFT JOIN CTB_ESPECIE_SERIE AS ES ON
						ES.ESPECIE_SERIE			=	S.ESPECIE_SERIE
					LEFT JOIN CADASTRO_CLI_FOR AS C ON
						LFE.COD_CLIFOR				=	C.COD_CLIFOR
				LEFT JOIN ENTRADAS B
					ON	LFE.NF_ENTRADA				= B.NF_ENTRADA			AND
						LFE.SERIE_NF_ENTRADA		= B.SERIE_NF_ENTRADA	AND
						C.NOME_CLIFOR				= B.NOME_CLIFOR
				LEFT JOIN NATUREZAS_ENTRADAS NE
					ON	B.NATUREZA					= NE.NATUREZA
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	IN		('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306')							AND
					LFE.ID_IMPOSTO				=		1										AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFE.CODIGO_FISCAL_OPERACAO

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LFE.CODIGO_FISCAL_OPERACAO,
					VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_ENTRADA LFE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	IN		('1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356')	AND
					LFE.ID_IMPOSTO				=		1												AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'					AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'			AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LFE.CODIGO_FISCAL_OPERACAO
			) A
			WHERE A.CODIGO_FISCAL_OPERACAO <> '|'
			GROUP BY A.CODIGO_FISCAL_OPERACAO
			ORDER BY A.CODIGO_FISCAL_OPERACAO

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E320
			SELECT
				REG				=	'|E320|',
				IND_OPER,
				UF				=	ISNULL(RTRIM(LTRIM(A.UF)), '') + '|',
				VL_CONT			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_CONTABIL), 15, 2))), '0.00') + '|',
				VL_BC_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.BASE_IMPOSTO_ICMS), 15, 2))), '0.00') + '|',
				VL_ICMS			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ICMS), 15, 2))), '0.00') + '|',
				VL_ST			=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ST), 15, 2))), '0.00') + '|',
				VL_COMPL		=	'0.00|',
				VL_ISNT_ICMS	=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_ISENTO_ICMS), 15, 2))), '0.00') + '|',
				VL_OUT_ICMS		=	ISNULL(RTRIM(LTRIM(STR(SUM(A.VALOR_IMPOSTO_OUTROS_ICMS), 15, 2))), '0.00') + '|'
			FROM (
					SELECT
						IND_OPER					=	'0|',
						UF							=	C.UF,
						VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL  ELSE 0.00 END),
						BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO  ELSE 0.00 END),
						VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
						VALOR_IMPOSTO_ST			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 12 THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
						VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
						VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
					FROM LF_REGISTRO_ENTRADA AS LFE
						LEFT JOIN CADASTRO_CLI_FOR AS C ON
							LFE.COD_FILIAL = C.COD_CLIFOR
					WHERE
						LFE.CODIGO_FISCAL_OPERACAO	NOT IN	('1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
															'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
															'1932', '2932')							AND
						LFE.ID_IMPOSTO				IN		(1, 12)									AND
						LFE.NOTA_CANCELADA			=		0										AND
						LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
						LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					GROUP BY
						C.UF

					UNION ALL

					SELECT
						IND_OPER					=	'1|',
						UF							=	C.UF,
						VALOR_CONTABIL				=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_CONTABIL  ELSE 0.00 END),
						BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.BASE_IMPOSTO  ELSE 0.00 END),
						VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
						VALOR_IMPOSTO_ST			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 12 THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
						VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
						VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
					FROM LF_REGISTRO_SAIDA LFS
						LEFT JOIN CADASTRO_CLI_FOR AS C ON
							LFS.COD_FILIAL = C.COD_CLIFOR
					WHERE
						LFS.CODIGO_FISCAL_OPERACAO	NOT IN	('5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
															'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
															'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
															'5932', '6932')							AND
						LFS.ID_IMPOSTO				IN		(1, 12)									AND
						LFS.NOTA_CANCELADA			=		0										AND
						LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
						LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
					GROUP BY
						C.UF
			) A
			GROUP BY A.IND_OPER, A.UF
			ORDER BY A.IND_OPER, A.UF

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E330
			SELECT
				REG				=	'|E330|',
				IND_TOT			=	ISNULL(A.CODIGO_FISCAL_OPERACAO, '') + '|',
				VL_CONT			=	SUM(A.VALOR_CONTABIL),
				VL_BC_ICMS		=	SUM(A.BASE_IMPOSTO_ICMS),
				VL_ICMS			=	SUM(A.VALOR_IMPOSTO_ICMS),
				VL_ST			=	SUM(A.VALOR_IMPOSTO_ST),
				VL_COMPL		=	0.00,
				VL_ISNT_ICMS	=	SUM(A.VALOR_IMPOSTO_ISENTO_ICMS),
				VL_OUT_ICMS		=	SUM(A.VALOR_IMPOSTO_OUTROS_ICMS)
			FROM (
				SELECT
					CODIGO_FISCAL_OPERACAO		=	LEFT(LFE.CODIGO_FISCAL_OPERACAO, 1),
					VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 12 THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_ENTRADA AS LFE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	NOT IN	('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306',
														'1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356',
														'1650', '2650', '3650', '1651', '2651', '3651', '1652', '2652', '3652', '1653', '2653', '3653', '1658', '2658',
														'1659', '2659', '1660', '2660', '1661', '2661', '1662', '2662', '1663', '2663', '1664', '2664',
														'1932', '2932')							AND
					LFE.ID_IMPOSTO				IN		(1, 12)									AND
					LFE.NOTA_CANCELADA			=		0										AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LEFT(LFE.CODIGO_FISCAL_OPERACAO, 1)

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LEFT(LFS.CODIGO_FISCAL_OPERACAO, 1),
					VALOR_CONTABIL				=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 12 THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_SAIDA AS LFS
				WHERE
					LFS.CODIGO_FISCAL_OPERACAO	NOT IN	('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
														'5256', '6256', '5257', '6257', '5258', '6258',
														'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
														'5306', '6306', '5307', '6307',
														'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
														'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
														'7350', '7358',
														'5650', '6650', '7650', '5651', '6651', '7651', '5652', '6652', '5653', '6653', '5654', '6654', '7654', '5655',
														'6655', '5656', '6656', '5657', '6657', '5658', '6658', '5659', '6659', '5660', '6660', '5661', '6661', '5662',
														'6662', '5663', '6663', '5664', '6664', '5665', '6665', '5666', '6666',
														'5932', '6932')							AND
					LFS.ID_IMPOSTO				IN		(1, 12)									AND
					LFS.NOTA_CANCELADA			=		0										AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LEFT(LFS.CODIGO_FISCAL_OPERACAO, 1)

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LEFT(LFS.CODIGO_FISCAL_OPERACAO, 1),
					VALOR_CONTABIL				=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_SAIDA AS LFS
				WHERE
					LFS.SERIE_NF			=		'D1'									AND
					LFS.NOTA_CANCELADA		=		0										AND
					LFS.ID_IMPOSTO			=		1										AND
					LFS.COD_FILIAL			=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LEFT(LFS.CODIGO_FISCAL_OPERACAO, 1)

				UNION ALL

				SELECT
					LEFT(LCFT_ICMS.CFOP, 1) AS CODIGO_FISCAL_OPERACAO, LCFT_ICMS.VL_BC_ICMS AS VALOR_CONTABIL, LCFT_ICMS.VL_BC_ICMS AS BASE_IMPOSTO_ICMS,
					LCFT_ICMS.VL_ICMS AS VALOR_IMPOSTO_ICMS, 0 AS VALOR_IMPOSTO_ST,
					LCFT_ICMS.VL_ISEN AS VALOR_IMPOSTO_ISENTO_ICMS, 0 AS VALOR_IMPOSTO_OUTROS_ICMS
				FROM LOJA_CONTROLE_FISCAL AS LCF (NOLOCK)
					LEFT JOIN CTB_ESPECIE_SERIE ESP (NOLOCK)
						ON LCF.ESPECIE_SERIE = ESP.ESPECIE_SERIE
					JOIN LOJAS_VAREJO AS FIL (NOLOCK)
						ON LCF.CODIGO_FILIAL = FIL.CODIGO_FILIAL
					JOIN FILIAIS (NOLOCK)
						ON FILIAIS.FILIAL = FIL.FILIAL
					LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
									LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF,
									VL_ISSQN = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
														ELSE
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
														END)
									FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
									LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
										A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
										A.TERMINAL 				= LCFT.TERMINAL 			AND
										A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
										A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
										A.ECF					= LCFT.ECF					AND
										A.SERIE_NF				= LCFT.SERIE_NF				AND
										A.CF_INICIAL			= LCFT.CF_INICIAL
									JOIN LOJAS_VAREJO G ON
										A.CODIGO_FILIAL	= G.CODIGO_FILIAL
									JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
										G.FILIAL 			= D.NOME_CLIFOR
									CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
									LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
										C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
									LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
									WHERE LCFT.ID_IMPOSTO IN (14, 22)
									GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
									LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF) AS LCFT_ISS
					ON LCF.CODIGO_FILIAL = LCFT_ISS.CODIGO_FILIAL
						AND LCF.TERMINAL = LCFT_ISS.TERMINAL
						AND LCF.ECF = LCFT_ISS.ECF
						AND LCF.ID_EQUIPAMENTO = LCFT_ISS.ID_EQUIPAMENTO
						AND LCF.DATA_FISCAL = LCFT_ISS.DATA_FISCAL
						AND LCF.CF_INICIAL = LCFT_ISS.CF_INICIAL
						AND LCF.SERIE_NF = LCFT_ISS.SERIE_NF
					LEFT JOIN (SELECT LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
									LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.LEGENDA_TARIFA, LCFT.ID_IMPOSTO,
									CFOP = LCFT.CODIGO_FISCAL_OPERACAO,
									ALIQ_ICMS = LCFT.PER_TARIFA,
									VL_ICMS = SUM(CASE WHEN Y.ARREDONDA_NA_LOJA = '.T.' THEN
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE CAST(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END AS NUMERIC(19,2)) END
														ELSE
															CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN 0 ELSE ISNULL(LCFT.TOTAL_TARIFA*(LCFT.PER_TARIFA/100),0) END,2,2) END
														END),
									VL_ISEN = SUM(LCFT.VALOR_ISENTO),
									VL_BC_ICMS = CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ISNULL(LCFT.TOTAL_TARIFA,0)+
														CASE WHEN L.LX_CODIGO_APURACAO IS NULL THEN 0.00 ELSE ROUND(CASE WHEN D.INDICADOR_FISCAL_TERCEIRO = 6 THEN ISNULL(LCFT.TOTAL_TARIFA,0) ELSE
															CASE WHEN ISNULL(A.NOTA_CANCELADA,0) = 0 THEN ISNULL(CASE WHEN (A.TOTAL_ISENTO+A.TOTAL_NAO_TRIBUTAVEL+A.TOTAL_SUBSTITUICAO)-Z.VALOR_OUTROS_TARIFAS-Z.VALOR_ISENTO_TARIFAS < 0 THEN 0.00 ELSE (A.TOTAL_ISENTO+A.TOTAL_NAO_TRIBUTAVEL+A.TOTAL_SUBSTITUICAO)-Z.VALOR_OUTROS_TARIFAS-Z.VALOR_ISENTO_TARIFAS END *((100*(CASE WHEN LCFT.TOTAL_TARIFA = 0 THEN 1 ELSE LCFT.TOTAL_TARIFA END)/
																		CASE WHEN (SELECT SUM(TOTAL_TARIFA)
																	FROM LOJA_CONTROLE_FISCAL_TARIFAS (NOLOCK)
																	WHERE CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
																				TERMINAL 		= LCFT.TERMINAL 			AND
																				DATA_FISCAL 	= LCFT.DATA_FISCAL			AND
																				ID_EQUIPAMENTO	= LCFT.ID_EQUIPAMENTO		AND
																				ECF				= LCFT.ECF					AND
																				SERIE_NF		= LCFT.SERIE_NF				AND
																				CF_INICIAL		= LCFT.CF_INICIAL			AND
																				ID_IMPOSTO		= 1) = 0 THEN 1 ELSE
																	(SELECT SUM(TOTAL_TARIFA)
																	FROM LOJA_CONTROLE_FISCAL_TARIFAS (NOLOCK)
																	WHERE CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
																				TERMINAL 		= LCFT.TERMINAL 			AND
																				DATA_FISCAL 	= LCFT.DATA_FISCAL			AND
																				ID_EQUIPAMENTO	= LCFT.ID_EQUIPAMENTO		AND
																				ECF				= LCFT.ECF					AND
																				SERIE_NF		= LCFT.SERIE_NF				AND
																				CF_INICIAL		= LCFT.CF_INICIAL			AND
																				ID_IMPOSTO		= 1	) END
																	)/100),0) ELSE 0.00 END
														END ,2,2) END END+ISNULL(LCFT.VALOR_OUTROS,0.00)+ISNULL(LCFT.VALOR_ISENTO,0.00)
									FROM LOJA_CONTROLE_FISCAL A (NOLOCK)
										LEFT JOIN LOJA_CONTROLE_FISCAL_TARIFAS LCFT (NOLOCK) ON
											A.CODIGO_FILIAL 		= LCFT.CODIGO_FILIAL 		AND
											A.TERMINAL 				= LCFT.TERMINAL 			AND
											A.DATA_FISCAL 			= LCFT.DATA_FISCAL			AND
											A.ID_EQUIPAMENTO		= LCFT.ID_EQUIPAMENTO		AND
											A.ECF					= LCFT.ECF					AND
											A.SERIE_NF				= LCFT.SERIE_NF				AND
											A.CF_INICIAL			= LCFT.CF_INICIAL
										LEFT JOIN (SELECT Y.CODIGO_FILIAL,
																Y.TERMINAL,
																Y.DATA_FISCAL,
																Y.ID_EQUIPAMENTO,
																Y.ECF,
																Y.SERIE_NF,
																Y.CF_INICIAL,
																VALOR_ISENTO_TARIFAS		= SUM(ISNULL(VALOR_ISENTO,0.00)),
																VALOR_OUTROS_TARIFAS		= SUM(ISNULL(VALOR_OUTROS,0.00))
															FROM LOJA_CONTROLE_FISCAL_TARIFAS Y
														GROUP BY Y.CODIGO_FILIAL, Y.TERMINAL, Y.DATA_FISCAL, Y.ID_EQUIPAMENTO, Y.ECF, Y.SERIE_NF,	Y.CF_INICIAL) Z ON
											A.CODIGO_FILIAL 		= Z.CODIGO_FILIAL 		AND
											A.TERMINAL 				= Z.TERMINAL 			AND
											A.DATA_FISCAL 			= Z.DATA_FISCAL			AND
											A.ID_EQUIPAMENTO		= Z.ID_EQUIPAMENTO		AND
											A.ECF					= Z.ECF					AND
											A.SERIE_NF				= Z.SERIE_NF			AND
											A.CF_INICIAL			= Z.CF_INICIAL
										JOIN LOJAS_VAREJO G ON
											A.CODIGO_FILIAL	= G.CODIGO_FILIAL
										JOIN CADASTRO_CLI_FOR D (NOLOCK) ON
											G.FILIAL 			= D.NOME_CLIFOR
										CROSS JOIN (SELECT ISNULL(VALOR_ATUAL,'.F.') AS ARREDONDA_NA_LOJA FROM PARAMETROS WHERE PARAMETRO = 'ARREDONDA_NA_LOJA' ) Y
										LEFT JOIN CTB_LX_NATUREZAS_OPERACAO C (NOLOCK) ON
											C.CODIGO_FISCAL_OPERACAO 	= ISNULL(LCFT.CODIGO_FISCAL_OPERACAO,'5102')
										LEFT JOIN LF_LX_CODIGO_APURACAO L (NOLOCK) ON C.LX_CODIGO_APURACAO = L.LX_CODIGO_APURACAO
									WHERE LCFT.ID_IMPOSTO  = 1
									GROUP BY LCFT.CODIGO_FILIAL, LCFT.TERMINAL, LCFT.ECF, LCFT.ID_EQUIPAMENTO,
										LCFT.DATA_FISCAL, LCFT.CF_INICIAL, LCFT.SERIE_NF, LCFT.CODIGO_FISCAL_OPERACAO,
										LCFT.PER_TARIFA, L.LX_CODIGO_APURACAO, LCFT.TOTAL_TARIFA,
										D.INDICADOR_FISCAL_TERCEIRO, A.TOTAL_ISENTO, A.TOTAL_NAO_TRIBUTAVEL,
										A.TOTAL_SUBSTITUICAO, Z.VALOR_OUTROS_TARIFAS, Z.VALOR_ISENTO_TARIFAS,
										A.NOTA_CANCELADA, LCFT.VALOR_OUTROS, LCFT.VALOR_ISENTO, LCFT.LEGENDA_TARIFA, LCFT.ID_IMPOSTO) AS LCFT_ICMS
					ON LCF.CODIGO_FILIAL = LCFT_ICMS.CODIGO_FILIAL
						AND LCF.TERMINAL = LCFT_ICMS.TERMINAL
						AND LCF.ECF = LCFT_ICMS.ECF
						AND LCF.ID_EQUIPAMENTO = LCFT_ICMS.ID_EQUIPAMENTO
						AND LCF.DATA_FISCAL = LCFT_ICMS.DATA_FISCAL
						AND LCF.CF_INICIAL = LCFT_ICMS.CF_INICIAL
						AND LCF.SERIE_NF = LCFT_ICMS.SERIE_NF
					LEFT JOIN (SELECT A.CODIGO_FILIAL, A.TERMINAL, A.DATA_FISCAL, A.LEGENDA_TARIFA, A.ECF, A.ID_EQUIPAMENTO, A.CF_INICIAL, A.SERIE_NF, A.ID_IMPOSTO,
									 TOTAL_DESCONTO       = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_DESCONTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_DESCONTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_CANCELADO      = ROUND(CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN (TOTAL_CANCELADO / QTD) ELSE (TOTAL_CANCELADO * (TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA ) / 100.00 END, 2, 2),
		--							 TOTAL_CANCELADO      = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_CANCELADO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_CANCELADO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_BRUTO          = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_BRUTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_BRUTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_ISENTO         = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_ISENTO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_ISENTO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_NAO_TRIBUTAVEL = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_NAO_TRIBUTAVEL / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_NAO_TRIBUTAVEL * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END,
									 TOTAL_SUBSTITUICAO   = CASE WHEN (TOTAL_TARIFA = 0 AND SOMA_TOTAL_TARIFA = 0) THEN CAST((TOTAL_SUBSTITUICAO / QTD) AS DECIMAL(19,2)) ELSE CAST((TOTAL_SUBSTITUICAO * CAST((TOTAL_TARIFA * 100.00) / SOMA_TOTAL_TARIFA  AS DECIMAL(19,2))) / 100.00 AS DECIMAL(19,2)) END
								FROM LOJA_CONTROLE_FISCAL_TARIFAS A
									  JOIN (SELECT A.CODIGO_FILIAL, A.TERMINAL, A.ECF, A.ID_EQUIPAMENTO, A.DATA_FISCAL, A.CF_INICIAL, A.SERIE_NF,
											   SUM(TOTAL_TARIFA) AS SOMA_TOTAL_TARIFA, COUNT(*) AS QTD
											FROM LOJA_CONTROLE_FISCAL_TARIFAS A
											WHERE ID_IMPOSTO = 1
											GROUP BY A.CODIGO_FILIAL, A.TERMINAL, A.ECF, A.ID_EQUIPAMENTO, A.DATA_FISCAL, A.CF_INICIAL, A.SERIE_NF ) B ON
											 A.CODIGO_FILIAL      = B.CODIGO_FILIAL    AND
											 A.TERMINAL           = B.TERMINAL         AND
											 A.ECF                = B.ECF              AND
											 A.ID_EQUIPAMENTO     = B.ID_EQUIPAMENTO   AND
											 A.DATA_FISCAL        = B.DATA_FISCAL      AND
											 A.CF_INICIAL         = B.CF_INICIAL       AND
											 A.SERIE_NF           = B.SERIE_NF
										  JOIN LOJA_CONTROLE_FISCAL C ON
											 A.CODIGO_FILIAL      = C.CODIGO_FILIAL    AND
											 A.TERMINAL           = C.TERMINAL         AND
											 A.ECF                = C.ECF              AND
											 A.ID_EQUIPAMENTO     = C.ID_EQUIPAMENTO   AND
											 A.DATA_FISCAL        = C.DATA_FISCAL      AND
											 A.CF_INICIAL         = C.CF_INICIAL       AND
											 A.SERIE_NF           = C.SERIE_NF
										WHERE A.ID_IMPOSTO      = 1) AS LCF_TOTAL
					ON LCF_TOTAL.CODIGO_FILIAL		=	LCFT_ICMS.CODIGO_FILIAL		AND
						LCF_TOTAL.TERMINAL			=	LCFT_ICMS.TERMINAL			AND
						LCF_TOTAL.DATA_FISCAL		=	LCFT_ICMS.DATA_FISCAL		AND
						LCF_TOTAL.LEGENDA_TARIFA	=	LCFT_ICMS.LEGENDA_TARIFA	AND
						LCF_TOTAL.ECF					=	LCFT_ICMS.ECF			AND
						LCF_TOTAL.ID_EQUIPAMENTO	=	LCFT_ICMS.ID_EQUIPAMENTO	AND
						LCF_TOTAL.CF_INICIAL			=	LCFT_ICMS.CF_INICIAL	AND
						LCF_TOTAL.SERIE_NF			=	LCFT_ICMS.SERIE_NF			AND
						LCF_TOTAL.ID_IMPOSTO			=	LCFT_ICMS.ID_IMPOSTO
				WHERE
					(RTRIM(ESP.DESC_ESPECIE_SERIE) = 'CF' OR  RTRIM(ESP.DESC_ESPECIE_SERIE) = 'ECF' OR ESP.DESC_ESPECIE_SERIE IS NULL)
					--(RTRIM(ESP.DESC_ESPECIE_SERIE) IN ('CF', 'ECF', NULL))
					AND LCF.ECF <> 0
					AND LCF.SERIE_NF <> 'D1'
					AND FILIAIS.MATRIZ_FISCAL = '<<Alltrim(.PX_MATRIZ_FISCAL)>>'
					AND LCF.DATA_FISCAL BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LEFT(LFE.CODIGO_FISCAL_OPERACAO, 1),
					VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_ENTRADA AS LFE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	IN		('1250', '2250', '3250', '1251', '2251', '3251', '1252', '2252', '1253', '2253', '1254', '2254', '1255', '2255',
														'1256', '2256', '1257', '2257',
														'1300', '2300', '3300', '1301', '2301', '3301', '1302', '2302', '1303', '2303', '1304', '2304', '1305', '2305',
														'1306', '2306')							AND
					LFE.ID_IMPOSTO				=		1										AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFE.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LEFT(LFE.CODIGO_FISCAL_OPERACAO, 1)

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LEFT(LFS.CODIGO_FISCAL_OPERACAO, 1),
					VALOR_CONTABIL				=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFS.ID_IMPOSTO = 1  THEN LFS.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_SAIDA AS LFS
				WHERE
					LFS.CODIGO_FISCAL_OPERACAO	IN		('5250', '6250', '7250', '5251', '6251', '7251', '5252', '6252', '5253', '6253', '5254', '6254', '5255', '6255',
														'5256', '6256', '5257', '6257', '5258', '6258',
														'5300', '6300', '7300', '5301', '6301', '7301', '5302', '6302', '5303', '6303', '5304', '6304', '5305', '6305',
														'5306', '6306', '5307', '6307',
														'5350', '5351', '5352', '5353', '5354', '5355', '5356', '5357', '5932',
														'6350', '6351', '6352', '6353', '6354', '6355', '6356', '6357', '6932',
														'7350', '7358')							AND
					LFS.ID_IMPOSTO				=		1										AND
					LFS.ESPECIE					NOT IN	('ECF', 'CF')							AND
					LFS.SERIE_NF				<>		'D1'									AND
					LFS.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'			AND
					LFS.EMISSAO					BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'	AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LEFT(LFS.CODIGO_FISCAL_OPERACAO, 1)

				UNION ALL

				SELECT
					CODIGO_FISCAL_OPERACAO		=	LEFT(LFE.CODIGO_FISCAL_OPERACAO, 1),
					VALOR_CONTABIL				=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_CONTABIL ELSE 0.00 END),
					BASE_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.BASE_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ICMS			=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO ELSE 0.00 END),
					VALOR_IMPOSTO_ST			=	0.00,
					VALOR_IMPOSTO_ISENTO_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_ISENTO ELSE 0.00 END),
					VALOR_IMPOSTO_OUTROS_ICMS	=	SUM(CASE WHEN LFE.ID_IMPOSTO = 1  THEN LFE.VALOR_IMPOSTO_OUTROS ELSE 0.00 END)
				FROM LF_REGISTRO_ENTRADA AS LFE
				WHERE
					LFE.CODIGO_FISCAL_OPERACAO	IN		('1350', '1352', '1353', '1354', '1355', '1356', '1932',
														'2350', '2352', '2353', '2354', '2355', '2356', '2932',
														'3350', '3352', '3353', '3354', '3355', '3356')	AND
					LFE.ID_IMPOSTO				=		1												AND
					LFE.COD_FILIAL				=		'<<Alltrim(.PX_COD_CLIFOR)>>'					AND
					LFE.RECEBIMENTO				BETWEEN	'<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'			AND	'<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
				GROUP BY
					LEFT(LFE.CODIGO_FISCAL_OPERACAO, 1)
			) A
			WHERE A.CODIGO_FISCAL_OPERACAO <> '|'
			GROUP BY A.CODIGO_FISCAL_OPERACAO
			ORDER BY A.CODIGO_FISCAL_OPERACAO

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E340
		SELECT
			REG			=	'|E340|',
			COD_AJ		=	ISNULL(RTRIM(LTRIM(LF_SUB_ITEM_APURACAO.SUB_ITEM_APURACAO)), '') + '|',
			VL_AJ		=	ISNULL(LTRIM(RTRIM(STR(CTB_IMPOSTO_LANCAMENTO.VALOR_IMPOSTO, 15, 2))), '0,00') + '|',
			NUM_DA		=	'|',
			NUM_PROC	=	'|',
			IND_PROC	=	'0|',
			PROC7		=	'|',
			COD_INF_OBS	=	ISNULL(RTRIM(LTRIM(CTB_IMPOSTO_LANCAMENTO.OBS)), '') + '|'
		FROM CTB_LX_IMPOSTO_TIPO
		JOIN CTB_IMPOSTO_LANCAMENTO
			ON CTB_LX_IMPOSTO_TIPO.ID_IMPOSTO = CTB_IMPOSTO_LANCAMENTO.ID_IMPOSTO
		JOIN W_CTB_LANCAMENTO_ITEM
			ON  W_CTB_LANCAMENTO_ITEM.EMPRESA = CTB_IMPOSTO_LANCAMENTO.EMPRESA
				AND W_CTB_LANCAMENTO_ITEM.LANCAMENTO = CTB_IMPOSTO_LANCAMENTO.LANCAMENTO
				AND W_CTB_LANCAMENTO_ITEM.ITEM = CTB_IMPOSTO_LANCAMENTO.ITEM
		JOIN LF_SUB_ITEM_APURACAO
			ON CTB_IMPOSTO_LANCAMENTO.ID_SUB_ITEM_APURACAO = LF_SUB_ITEM_APURACAO.ID_SUB_ITEM_APURACAO
		LEFT JOIN UNIDADES_FEDERACAO
			ON LF_SUB_ITEM_APURACAO.UF = UNIDADES_FEDERACAO.UF
		JOIN LF_LX_CODIGO_APURACAO
			ON LF_LX_CODIGO_APURACAO.LX_CODIGO_APURACAO = LF_SUB_ITEM_APURACAO.LX_CODIGO_APURACAO
		WHERE
			CTB_IMPOSTO_LANCAMENTO.ID_IMPOSTO = 1
			AND W_CTB_LANCAMENTO_ITEM.COD_FILIAL = '<<Alltrim(.PX_COD_CLIFOR)>>'
			AND W_CTB_LANCAMENTO_ITEM.DATA_LANCAMENTO BETWEEN '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>' AND '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'

----------------------------------------------------------------------------------------------------------------------------------------------------
-- E360
		SELECT A.LX_CODIGO_APURACAO, VALOR_ITEM	= SUM(A.VALOR_ITEM)
		FROM  W_LF_APURACAO_ITEM A
		WHERE A.ID_IMPOSTO = 1
			AND A.COD_MATRIZ_FISCAL = '<<Alltrim(.PX_COD_CLIFOR)>>'
			AND A.DATA_INICIAL >= '<<Alltrim(Dtos(.PX_DATA_INICIAL))>>'
			AND A.DATA_FINAL <= '<<Alltrim(Dtos(.PX_DATA_FINAL))>>'
		GROUP BY A.LX_CODIGO_APURACAO
		ORDER BY A.LX_CODIGO_APURACAO
